Named routes in link_to wrapper


#1

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.


#2

I just wanted to bump this once, because I’m still very interested in
this, even though my workarounds are satisfactory.


#3

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


#4

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!