Search code examples
ruby-on-railshtml-helper

Ruby on Rails link_to route path while preserving original query string parameters


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


Solution

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