link_to acting strangely in Rails - ruby-on-rails

All my link_to's in my views seem to return the link text, but also the ink address in brackets. Why is this?
E.g
<%= link_to "Home", root_url %>
and renders in the view
Home (http://localhost:3000/)

Check your stylesheets. For example, I know plenty of print media css does that for the benefit of those who can't "click" their paper.

Related

Using `_path` in Rails Mailer

I'm currently working with a mailer in which I need to link an index view. In my app, this is reachable by reminders_path, but when I used that in my mailer (<%= link_to "Memory Enhancer", reminders_path %>) the link didn't work.
After some googling and some SO searches, most things seemed to recommend something like this:
<%= link_to "Take Me To My Memory Enhancer", Rails.application.routes.url_helpers.url_for(controller: "reminders", action: "index") %>
However, the link still isn't functional. What is the "proper" rails way to do this?
Use reminders_url, as you want a full url, not just the path.

How to create a href tag with link_to and action 'create'

I want to create a href tag like href=contacts/create. In my contacts_controller, I have a create GET action. I know this is against rails convention. I still need to create the above link using options = {controller=> 'contacts', action=>'create'}. It works for any other arbitrary action name
You can the hardcoded path option:
<%= link_to "Create", "contacts/create" %>
or the Rails generated path option:
<%= link_to "Create", { controller: "contacts", action: "create" } %>
This is not just against Rails' convention, but against sounds HTTP usage. This often causes serious problems that you can't predict in advance. Web crawling is just one of them, where something like the Google bot accidentally creates a new contact in your database, simply by crawling the page. Or script kiddies who find you have a create link, and send 100,000 clicks to it in quick succession.
Numerous other issues happen like this, including, at one well-known time, Google Chrome pre-fetching GET urls from the page to "speed up the user experience"; this was felt far and wide by sites that had used this technique. It's not an idle warning or a style issue: this can have a disastrous impact on your site.
First off this is really bad idea since GET requests should be idempotent. You're not just flouting convention - you're setting yourself and your users up for a really bad time since for example pressing the back and forward buttons will cause resources to be created - over and over. And there is guaranteed a better way to solve whatever you are trying to do such as:
# a "discrete form"
<%= button_to "Create contact", contacts_path, method: :post %>
# or use the rails ujs
<%= link_to "Create contact", contacts_path, method: :post %>
If you ABSOLUTELY have do this:
Rails.application.routes.draw do
get "contacts/create"
end
You can now do:
<%= link_to "Create", { controller: 'contacts', action: 'create' } %>
Congratulations, you broke the internets.
Like you mentioned, this is against rails convention, but if absolutely necessary, you can do this from your controller:
options = {controller=> 'contacts', action=>'create'}
view_context.link_to url_for(options)
If you need the href to only be the path, you can do:
options = {controller=> 'contacts', action=>'create'}
view_context.link_to url_for(options.merge(only_path: true))

How to get root path of rails app

I am novice to rails. I have been using LAMP for a long time. I am looking for something like {{URL::to('/')}} from Laravel.
I DO NOT want to use link_to which creates entire anchor tag as it defeats the purpose of isolating views. I want to get a value to put in href tag.
This is a link
Rails has has the root_path and root_url helpers
You also seem quite confused about link_to it does not compromise the "isolation" of your views. Its a helper method which makes it possible to create very dynamic html without doing a lot of interpolation.
<%= link_to t('home'), root_path, class: 'btn btn-success' %>

Simple link_to from root_url

I have a very simple view link_to problem no error message just a strange dot in url "http://0.0.0.0:3000/.Hire" not sure why the dot is there between the root url and Hire?
link_to:
<%= link_to page.name, root_url(page.name) %>
The routes are fine if I type manually: http://0.0.0.0:3000/Hire
I get taken to the right page but the link_to is just wrong.
I would be grateful for any help.
Many thanks
Dan
root_url isn't supposed to be given this kind of parameters. It's logic it's failing.
Run rake_routes in your console to get the proper names of your routes.
One ugly workaround to fit your needs would be:
<%= link_to page.name, root_url + page.name %>
One last question: why do you use root_url instead of root_path?
The former tend to uselessly pollute the views.

Linking Text Logo and span

how do i link my Logo which is made out of Text and CSS to my root_path and the inside of it?
This is how it would look without Rails:
<h1><a href=#>Main Title<span>subtitle</span></a></h1>
I am using no images and just plain CSS text for the Logo.
Thank you!
If you mean link to root page:
<h1><%= link_to 'Main Title<span>subtitle</span>'.html_safe, '/' %></h1>
If you want other locations and have some resources set up, you could use paths:
<h1><%= link_to 'Main Title<span>subtitle</span>'.html_safe, example_path %></h1>
If you happen to need even more HTML markup in the link, then check this question out:
How do I wrap link_to around some html ruby code?

Resources