# Named routes in link\_to wrapper

**URL:** <https://www.ruby-forum.com/t/named-routes-in-link-to-wrapper/166316>\
**Category:** Rails\
**Created:** [April 23, 2009, 8:26pm UTC](https://www.ruby-forum.com/t/named-routes-in-link-to-wrapper/166316 "2009-04-23T20:26:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aawilson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e495f1/32.png) [@aawilson](https://www.ruby-forum.com/u/aawilson)\
**Post date:** [April 23, 2009, 8:26pm UTC](https://www.ruby-forum.com/t/named-routes-in-link-to-wrapper/166316/1 "2009-04-23T20:26:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![aawilson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e495f1/32.png) [@aawilson](https://www.ruby-forum.com/u/aawilson)\
**Post date:** [April 28, 2009, 10:06pm UTC](https://www.ruby-forum.com/t/named-routes-in-link-to-wrapper/166316/2 "2009-04-28T22:06:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aawilson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e495f1/32.png) [@aawilson](https://www.ruby-forum.com/u/aawilson)\
**Post date:** [April 29, 2009, 12:41am UTC](https://www.ruby-forum.com/t/named-routes-in-link-to-wrapper/166316/3 "2009-04-29T00:41:20Z")

</div>

On Apr 23, 7:26 pm, Aa Wilson [removed\_email\_address@domain.invalid](mailto: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

---

<div class="post-metadata">

**Author:** ![aawilson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e495f1/32.png) [@aawilson](https://www.ruby-forum.com/u/aawilson)\
**Post date:** [April 29, 2009, 2:52am UTC](https://www.ruby-forum.com/t/named-routes-in-link-to-wrapper/166316/4 "2009-04-29T02:52:43Z")

</div>

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!
