Hello all. This is a rails 3 oriented question.
I’m creating a simple, application-wide view-helper method in my app/helpers/application_helper.rb that will be called in the context of several different controller/action views.
module ApplicationHelper def my_custom_link_to link_to “Here Silly” end end
As you can see, by NOT providing any link-location (url_for) options, my little helper will default to simply creating links to the current page (uses the current controller and action to generate the link). This is exactly what I want…
Except (I’m sure you saw this coming): I want the generated link to always be identical (except for query string parameters) to the current URL as shown in the browser’s window. Normally, this is already the case for default resource-routed links. However, I’ve got some resources available through several (aliased?) routes. Example (config/routes.rb):
… resources :entities resources :rolodex, :controller => :entities …
When I’m using my helper in my app/views/entities/index.html.erb, the path in the generated link is always /entities, regardless of whether the page was accessed through the /rolodex or /entities route. I’d like the path to be /rolodex when the page is requested using this path and /entities when /entities is requested. And just FYI, changing the fact that my “entity” resource is available via these multiple routes isn’t an option (and this isn’t the only resource we have with such aliasing).
The technique that (seems) to work the way I want it, it to change my method above to use request.fullpath:
link_to "Here Silly", request.fullpath
Though this works, I’m wondering if there is a more idiomatic way to accomplish this. So, is there?