4

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

3 Answers 3

5
<%= 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!

Sign up to request clarification or add additional context in comments.

Comments

1

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) %>

Comments

0
<%= 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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.