Most idiomatic way to link_to the current page

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:

def my_custom_link_to

link_to "Here Silly", request.fullpath

end

Though this works, I’m wondering if there is a more idiomatic way to accomplish this. So, is there?

Thanks!

Just a follow-up to my own post, another reason I’m looking for an alternative method here (besides being more idiomatic) is to support a version that can add query-string parameters (and a fragment identifier) to the generated url:

Example Version 2:

module ApplicationHelper def my_custom_link_to(my_extra_params = {}) # messy having to do this by hand… fragment = my_extra_params.delete(:fragment) utils = Rack::Utils # shortcut variable query = my_extra_params.map { |k, v| “#{utils.escape(k.to_s)}=#{utils.escape(v.to_s)}” }.join(“&”) path = request.fullpath

path    += "?#{query}" unless query.blank?
path    += "##{fragment}" unless fragment.blank?
link_to "Here Silly", path

end end

Again, this works but look how friggin messy it is. Is there some kind of cool variation of #link_to I don’t know about that accepts a symbol :self instead of an AR model, hash, or string? Just curious.

Okay, since I’ve got no response and since the traffic here is high enough that this topic will be effectively “buried” soon, I’ll follow up with my final solution.

I decided what would be nice is a universal #link_to_self helper method. The idea is that it otherwise works just like the existing #link_to family of helpers (and in fact is coded based on their implementations). The difference is that you simply don’t provided the “where” aspect, since you’re linking to the same page. The improvement over simply using #link_to is, as I described above, that the existing path will be re-used instead of the default route in cases where multiple routes alias the same controller/action pair.

My final implementation in my ApplicationHelper class (app/helpers/application_helper.rb):