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

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

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

On 4/9/07, Pat M. [email protected] wrote:

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 D.

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

Pat M. wrote:

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
stuff about old cars

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

On 4/9/07, Andy D. [email protected] 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

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

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

On 4/10/07, Chad [email protected] 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