Forum: Ruby on Rails Named routes in link_to wrapper

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Aa W. (Guest)
on 2009-04-23 22:26
I am trying to create a custom link_to helper that passes sort
parameters to a link.  It has the following code, which is pretty much
just a rewrite of link_to:

  def sort_link_to(sort, old_sort, old_dir, *args, &block)
    if block_given?
      options = (args.first || {}).merge({
        :old_sort => old_sort,
        :old_dir => old_dir,
        :sort => sort
      })
      html_options = args[1]
      link_to(capture(&block),options,html_options)
    else
      name = args.first
      options = (args[1] || {}).merge({
        :old_sort => old_sort,
        :old_dir => old_dir,
        :sort => sort
      })

      html_options = args[2]

      link_to(name,options,html_options)
    end
  end

This works fine if I use it like so:

sort_link_to 'somesort','somename', :controller => 'somecontroller',
:action => 'someaction'

My question is, how do I write this in such a way that I can use named
routes, i.e., so that either of the following works?

This

sort_link_to 'somesort','somename', @someobjectwithmappedresources

or this

sort_link_to 'somesort','somename', somemappedresource_path

Currently, if I do either, it fails when it tries to merge the generated
path from the named route with the options given to the helper (which
makes sense).  If there is some method that will take a hash and convert
it to a query string that I may then concatenate to the resource path,
then that would work, but I can not find such a method.

Also, and if very, very secondary importance, is there a way to access
controller instance variables in a helper?

Thanks in advance for your time.
Aa W. (Guest)
on 2009-04-29 00:06
I just wanted to bump this once, because I'm still very interested in
this, even though my workarounds are satisfactory.
Frederick C. (Guest)
on 2009-04-29 02:41
(Received via mailing list)
On Apr 23, 7:26 pm, Aa Wilson <removed_email_address@domain.invalid>
wrote:
> path from the named route with the options given to the helper (which
> makes sense).  If there is some method that will take a hash and convert
> it to a query string that I may then concatenate to the resource path,
> then that would work, but I can not find such a method.

Well given a record you can call polymorphic_path on it (optionally
supplying options to merge in). Hash has a to_query method on it that
will generate a query string.

>
> Also, and if very, very secondary importance, is there a way to access
> controller instance variables in a helper?
>
should just work.

Fred
Aa W. (Guest)
on 2009-04-29 04:52
Oh yes, I had forgotten that I had solved the second problem.  I
actually made accessors, then used the make_helper function.

to_query sounds delectable, and I'll take a look at polymorphic_path.
Thanks a bundle!
This topic is locked and can not be replied to.