How do I create a hyperlink without displaying the URL path?
In my view code, I have something similar to this:
<% #users.each do |user| %>
<%= link_to user.lname, [:edit, user] %>
...
<% end %>
This code works, but produces something like:
Smith (url path)
I just want "Smith"
Not sure exactly what you have going on, or are going for with that array object as the URL target, but I'm betting this will solve your problems:
<%= link_to user.lname, edit_user_path(user) %>
Rails link_to is simply a Helper to create an HTML anchor tag.
You can see in the ActionView Helper Docs that link_to takes 3 parameters.
link_to(body, url, html_options = {})
This generates something similar to...
Smith
If you wish to change your routes, can you give more information like the full URL that's displaying, your routes.rb files, and the rake routes results?
Found the problem. It was in my lousy css. My css was placing the url in the title of the anchor text. I learned a lot about link_to also.
Related
I'd like to get the paramenter from the URL on my view/html.
For example, I'm showing an specific item from my data base, and the URL is like this: "http://localhost:3000/menus/index.%23%3CMenu::ActiveRecord_Relation:0x007f50ed153250%3E?id=6"
What I want is: when I click on the New Button of that specific item, the form opens with the URL "http://localhost:3000/menus/new", but I want somehow pass that id=6 to this NEW view.
I am using this code on the html to open the form: <%= link_to 'Novo', new_menu_path %>
I also tried: <%= link_to 'Novo', new_menu_path(#menus, :id) %>, but the id param is always null/empty.
How do I do that?
Thank you
To pass an extra param to an url when you define a link_to
<%= link_to 'Novo', new_menu_path(id: #menu.id, other_param: "hello") %>
will generate http://localhost:3000/menus/new?id=6&other_param=hello
this will add the following to the params hash
Parameters: {"id"=>"6", "other_param"=>"hello"}
well :id is just a symbol, you need to tell the route helper what to bind to it. For example
<%= link_to 'Novo', new_menu_path(id: params[:id]) %>
which should give you something like /menus/new?id=6
In Rails, I have a "notifications" class, one field of which is "link". The links contained within this class are formatted like: exchange_path(6), where that is the path to the show action in the exchange controller.
I'm now trying to output this link as such:
<%= link_to "View Exchange", notification.link %>
This line is in a loop which begins as such:
<% #notifications.each do |notification| %>
When I click this link, it takes me to localhost:3000/users/exchange_path(6) instead of localhost:3000/exchanges/6 like I would expect. (The loop generating the faulty link is on localhost:3000/users/2)
this could be scary...
<%= link_to "View Exchange", eval(notification.link) %>
should evaluate and use the path helpers. but you need to be 100% sure that nothing bad gets put in the link field..
You could do this:
<%= link_to("View Exchange", "/#{notification.link.gsub('(', '/').gsub(')', '').gsub('_path', 's')}") %>
or set up a method in your model that formats it for you:
def format_link
link.gsub('(', '/').gsub(')', '').gsub('_path', 's')
end
and just call that in your link_to:
link_to("View Exchanges", notification.format_link)
This will only work if all the links are formatted exactly as the example in the question
I want to generate the next html link:
http://url.com
To reproduce it using the link_to helper I have to write:
<%= link_to "http://url.com", "http://url.com" %>
What doesn't look DRY at all, I was expecting this to work:
<%= link_to "http://url.com" %>
But the above code generate a link targeting the actual request.url, not the one I'm sending in the param.
Am I missing something?
You're not missing anything --- the normal case is for the URL and the text that shows to the user to be different.
If you'd like, you could create a helper like
def link_to_href(link, args={})
link_to link, link, args
end
then, when you use it,
<%= link_to_href "http://url.com" %>
Will output
http://url.com
If you take a look at the source code of link_to you will see that at line 248 the a tag label is build with name || url.
That's why you have this behaviour and there is noway to do it like you're expecting.
i am totally new to rails programming... i used the request.path to get the current url and display it in all my views by specifying it in applications.html.erb. It is returning the entire path and i want to display it as a link... so i use
link_to to specify it as url..now here is what i want to do.. the url returned will be in the format path1/path2/path3..... i want to display it as path1>path2>path3 and as a link such that when the user clicks path1, it should take him to path 1 and so on...
this is the code i gave in html.erb file
but i get an error that says undefined method.... what should i do to accomplish that??
You could split the request.path on / and then build up the various links, but that could get really cluttered for deeply nested paths. I think a better approach would be to use something like breadcrumbs_on_rails and declare your breadcrumbs explicitly and render them in a partial or helper method. I think you could also use some Rails filter magic to have action names breadcrumbed automatically, but making the breadcrumbs explicit forces you to think about your site and your users more than programmatically vomiting out a string of links of unknown length.
You can do like this:
<% path = request.path %>
<% links = path.split('/') %>
<% ll="/" %>
<% links.each do |l| %>
<% ll += (l+'/') %>
<%= link_to l,ll %> >
<% end %>
I have implemented a search feature of my application. After submitting a search term, you are redirected to a url that looks something like this:
http://localhost:3000/search?utf8=✓&search=term
I want to be able to store this in the session and link to it later:
<%= link_to 'Back', recent_search_path %>
How might I achieve this?
Thanks to rc for referring me to a very similar question. This could be achieved by storing the current path in the session:
session[:recent_search_path] = request.url
...and retrieving it again in the view:
<%= link_to "Back to search results", session[:recent_search_path] %>