# How do I use link\_to with all the current options?

**URL:** <https://www.ruby-forum.com/t/how-do-i-use-link-to-with-all-the-current-options/91507>\
**Category:** Rails\
**Created:** [April 9, 2007, 11:47pm UTC](https://www.ruby-forum.com/t/how-do-i-use-link-to-with-all-the-current-options/91507 "2007-04-09T23:47:57Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 9, 2007, 11:47pm UTC](https://www.ruby-forum.com/t/how-do-i-use-link-to-with-all-the-current-options/91507/1 "2007-04-09T23:47:57Z")

</div>

In one of my pages I want to create some URLs that go to the same  
page, but tack on an extra parameter (column sorting stuff). How do I  
do that?

I’ve tried  
link\_to “Title”, :sort =\> “title”

and that’s pretty close…it keeps the controller name, but it doesn’t  
include one of the parameters. I’m using nested routes, so the URL  
should be  
[http://site/companies/1/videos?sort=title](http://site/companies/1/videos?sort=title)

but instead it’s just  
[http://site/videos?sort=title](http://site/videos?sort=title)

Thanks for any help.

Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 10, 2007, 12:01am UTC](https://www.ruby-forum.com/t/how-do-i-use-link-to-with-all-the-current-options/91507/2 "2007-04-10T00:01:21Z")

</div>

Pat M. wrote:

> In one of my pages I want to create some URLs that go to the same  
> page, but tack on an extra parameter (column sorting stuff). How do I  
> do that?
> 
> I’ve tried  
> link\_to “Title”, :sort =\> “title”
> 
> and that’s pretty close…it keeps the controller name, but it doesn’t  
> include one of the parameters. I’m using nested routes, so the URL  
> should be  
> [http://site/companies/1/videos?sort=title](http://site/companies/1/videos?sort=title)
> 
> but instead it’s just  
> [http://site/videos?sort=title](http://site/videos?sort=title)
> 
> Thanks for any help.
> 
> Pat

Your best bet is to just used the named route for the page your are on.

videos\_path(:company =\> @company, :sort =\> ‘title’)

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 10, 2007, 12:14am UTC](https://www.ruby-forum.com/t/how-do-i-use-link-to-with-all-the-current-options/91507/3 "2007-04-10T00:14:02Z")

</div>

On 4/9/07, Pat M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> should be  
> [http://site/companies/1/videos?sort=title](http://site/companies/1/videos?sort=title)
> 
> but instead it’s just  
> [http://site/videos?sort=title](http://site/videos?sort=title)
> 
> Thanks for any help.
> 
> Pat

You will definitely need an :id as well, like:  
link\_to “Title”, :sort =\> “title”, :id =\> 1

Without knowing how your controllers/actions are set up it is hard to  
say whether you need any other options. Something like:  
link\_to “Title”, :sort =\> “title”, :id =\> 1, :controller =\> “companies”

or something might be good, it should basically be like what your  
route looks like.

Also take a look at named routes for this kind of thing as well.

[http://api.rubyonrails.org/classes/ActionController/Routing.html](http://api.rubyonrails.org/classes/ActionController/Routing.html)

- Andy D.

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 10, 2007, 4:12am UTC](https://www.ruby-forum.com/t/how-do-i-use-link-to-with-all-the-current-options/91507/4 "2007-04-10T04:12:21Z")

</div>

Pat M. wrote:

> url options with another parameter.

Try link\_to “Title”, :overwrite\_params =\> { :sort =\> ‘title’ }

I know the :overwrite\_params option is valid with url\_for() and  
link\_to() relies  
on the method so it should work, in theory.

– Long  
[http://MeandmyCity.com/](http://MeandmyCity.com/) - Find your way  
[http://edgesoft.ca/blog/read/2](http://edgesoft.ca/blog/read/2) - No-Cookie Session Support plugin

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 10, 2007, 11:27am UTC](https://www.ruby-forum.com/t/how-do-i-use-link-to-with-all-the-current-options/91507/5 "2007-04-10T11:27:12Z")

</div>

Pat M. wrote:

> [http://site/companies/1/videos?sort=title](http://site/companies/1/videos?sort=title)

I believe you may need to explicitly include the controller, action etc.  
Are you using RESTful routes? If you are, you’re probably looking at  
something like link\_to videos\_path(:company\_id =\> 1, :sort =\>“title”). I  
think (coffee not kicked in yet).

Hope this helps  
Chris

* * *

[http://www.autopendium.co.uk](http://www.autopendium.co.uk)  
stuff about old cars

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 10, 2007, 12:39pm UTC](https://www.ruby-forum.com/t/how-do-i-use-link-to-with-all-the-current-options/91507/6 "2007-04-10T12:39:18Z")

</div>

See  
[http://api.rubyonrails.com/classes/ActionController/Base.html#M000262](http://api.rubyonrails.com/classes/ActionController/Base.html#M000262)

"If you explicitly want to create a URL that’s almost the same as the  
current URL, you can do so using the :overwrite\_params options. Say  
for your posts you have different views for showing and printing them.  
Then, in the show view, you get the URL for the print view like this

url\_for :overwrite\_params =\> { :action =\> ‘print’ }

This takes the current URL as is and only exchanges the action. In  
contrast, url\_for :action =\> ‘print’ would have slashed-off the path  
components after the changed action.

Chad

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 10, 2007, 12:55am UTC](https://www.ruby-forum.com/t/how-do-i-use-link-to-with-all-the-current-options/91507/7 "2007-04-10T00:55:06Z")

</div>

On 4/9/07, Andy D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > and that’s pretty close…it keeps the controller name, but it doesn’t
> 
> You will definitely need an :id as well, like:  
> link\_to “Title”, :sort =\> “title”, :id =\> 1
> 
> Without knowing how your controllers/actions are set up it is hard to  
> say whether you need any other options. Something like:  
> link\_to “Title”, :sort =\> “title”, :id =\> 1, :controller =\> “companies”

No, I want to keep the EXISTING url options on the page. So say for  
example, I’ve gone to

[http://site/companies/1/videos](http://site/companies/1/videos)

now I want to generate that exact same URL but add an extra parameter.  
I want to do this without explicitly setting the controller, action,  
IDs, etc. This is a template that we use in a lot of places, and now  
we want to add sorting. So basically I just want to merge the current  
url options with another parameter.

Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 10, 2007, 6:28pm UTC](https://www.ruby-forum.com/t/how-do-i-use-link-to-with-all-the-current-options/91507/8 "2007-04-10T18:28:38Z")

</div>

Wow. I’m really thankful to you kittens for posting about this. What are  
the  
chances that I’d need something like this on my app this morning?!  
Apparently 100%. Thanks a ton!

RSL

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 10, 2007, 9:27pm UTC](https://www.ruby-forum.com/t/how-do-i-use-link-to-with-all-the-current-options/91507/9 "2007-04-10T21:27:08Z")

</div>

On 4/10/07, Chad [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This takes the current URL as is and only exchanges the action. In  
> contrast, url\_for :action =\> ‘print’ would have slashed-off the path  
> components after the changed action.
> 
> Chad

Hey Chad, thanks for the helpful response.

Pat
