Reputation: 4937
I have a URL like so http://example.com/?sort=pop
In my view I am using link_to category.name, categories_path(category)
How can I preserve any query string parameters that might already exist on the requesting URL?
So the final link URL would be http://example.com/categories/1?sort=pop
Upvotes: 4
Views: 4158
Reputation: 5362
<%= link_to category.name, category_path(category,
request.parameters.merge({:new_params => 42}) ) %>
This should link to the right path, preserve existing parameters and add any new params you might have.
Rails: Preserving GET query string parameters in link_to
Upvotes: 0
Reputation: 7070
Anthony's solution almost worked for me. However, it didn't like just having params
as one of the passed through variables. Instead, I had to add params:
or :params =>
to the link. It works just find for me now.
<%= link_to "XLS ", users_path(format: "xls", params: params) %>
Upvotes: 1
Reputation: 10395
<%= link_to category.name, category_path(category, params) %>
Should do the trick
Take care that the default route helper to access a specific Category is category_path
. Singular since it's for only one category, makes sense!
Upvotes: 5