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
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.
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.
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.
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
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).
"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
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.
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!