How do I use link_to with all the current options?

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

but instead it's just   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

- Andy Delcambre

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

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

Pat Maddox wrote:

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

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.

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/ - Find your way http://edgesoft.ca/blog/read/2 - No-Cookie Session Support plugin

Pat Maddox 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

but instead it's just   http://site/videos?sort=title

Thanks for any help.

Pat

>

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

See 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

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

Hey Chad, thanks for the helpful response.

Pat