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 %>
Related
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.
I'm using middleman to generate a static webpage. I need to add a consistent but understandable string to all urls so I can understand how users navigate on the page.
Now i do it like this
<% link_to '/'+?button=navigation , class: 'logotype', itemprop: 'url' do %>
...
<% end %>
I would prefer not having to manually add all the parameters but rather just use something that's already there, like a scope or something. I was thinking about using the name of the template file for example. The url is not unique enough.
Any suggestions?
The standard way of doing this would be to write a helper method that encapsulates your functionality:
<%= link_to_as_nav('/', class: 'logotype', ...) do %>
...
<% end %>
Then write a helper method:
def link_to_as_nav(url, options)
link_to(url + '?button=navigation', options)
end
This is the naïve approach and won't account for a url argument that already has parameters added, but that's something you can work to fix.
I have a ruby on rails application, and want to display an image on the index page depending on what the URL is. If the url is: localhost:3000/products/multi_find I do not want to show the image, if it is anything else I do want it to be shown.
Is their a way I can retrieve the url and store it in a variable to run an <& if statement like: if url != localhost:3000/products/multi_find &><%= image tag "test.png", :size => "15x18" %> <% end %>
Thank you in advance.
None of those solutions worked, this did:
<% if request.original_url == 'http://localhost:3000/products/multi_find' %>
<%= image tag "test.png", :size => "15x18" %>
<% end %>
In Rails you must think in terms of routing rules, not URLs. An URL is nothing else than the result of a routing directive.
Therefore, /products/multi_find can be expressed in terms of action, controller and parameters.
With that in mind, you can use the current_page? helper to check if the current URL matches the route you expect, if it does then ignore the image.
You can even use rails "action_name" and "controller_name" helper methods to get the job done.
Try this:
<% unless current_page?('/products/multi_find') %>
<%= image tag "test.png", :size => "15x18" %>
<% end %>
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.