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_tois, 
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):

  #
  # This will create a link to the current document with the provided 
options
  # being added to the URL as query string parameters. You may either 
provide
  # the link text (the body) as the first parameter, or instead provide a 
block
  # that returns the content of the link. You may also provide a standard
  # html_options hash.
  #
  # In addition to adding any provided options as parameters to the query 
string
  # this will also add all query string parameters from the current request 
back
  # into the query string as well. To override an existing query string
  # parameter, simply provide an option of the same name (string or symbol 
form)
  # with the new value. If the new value is nil, the existing parameter will 
be
  # omitted. All non-string values will be converted using #to_s.
  #
  # Finally, you may provide an option with the key :fragment to specify a
  # fragment identifier you'd like to be appended onto the end of the URL. 
You
  # _must_ use the symbol :fragment, not the string "fragment" for the key,
  # otherwise the value will be added to the query string: "?fragment=value"
  #
  # == Signatures
  #
  #     link_to_self(body, options = {}, html_options = {})
  #       # body is the "name" (contents) of the link
  #
  #     link_to_self(options = {}, html_options = {}) do
  #       # link contents defined here
  #     end
  #
  def link_to_self(*args, &block)
    if block_given?
      options      = args.first || {}
      html_options = args.second
      link_to_self(capture(&block), options, html_options)
    else
      name         = args[0]
      options      = args[1] ? args[1].dup : {}
      html_options = args[2]
      fragment     = options.delete(:fragment)
      query_string = 
options.with_indifferent_access.reverse_merge(request.query_parameters).map 
{ |k, v|
        v.nil? ? nil : 
"#{Rack::Utils.escape(k.to_s)}=#{Rack::Utils.escape(v.to_s)}"
      }.compact.join("&")
      path  = request.path
      path += "?#{query_string}" unless query_string.blank?
      path += "##{fragment}"     unless fragment.blank?
      result = link_to(name, path, html_options)
      result
    end
  end

This is just for posterity and in case anyone else ever has a similar need. 
I personally would love it if this functionality was in rails itself 
(modification of #link_to, taking the :self symbol or simply acting this way 
in the absence of a url string, a resource record, or a #url_for options 
hash).

Seems to work great for what I need. Let me know anyone if rails already 
does this somewhere (I doubt it now that I've done significant digging). I'd 
also like recommendations on improvements that can be made within my 
definition above for it to be more idiomatic (not rely on request.path, 
request.query_parameters or use Rack::Utils.escape for example).

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to