Friday, March 25, 2011

[Rails] Re: Most idiomatic way to link_to the current page

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.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate