views:

65

answers:

3

I have a typical search facility in my app which returns a list of results that can be paginated, sorted, viewed with a different records_per_page value, etc. Each of these options is controlled by parameters in the query string. A simplified example:

/search?q=test&page=2

Now say I need to display a set of links that set records_per_page value to 10, 20, 30. Each link must include the existing query parameters, which can be a very long set, plus a new per_page parameter.

/search?q=test&page=2& ... &per_page=10
/search?q=test&page=2& ... &per_page=20
/search?q=test&page=2& ... &per_page=30

Is there an easy way to do it with just link_to helper or I need to parse and reproduce the query string from previous request somehow?

+1  A: 

You can just throw elements of the params hash at link_to. Like

link_to "some_other_link", "/search", :page => params[:page]
Tass
+1  A: 

What about

<%= link_to 'Whatever', :overwrite_params => { :pear_page => 20 } %>

?

jordinl
This looks just like what I was looking for. However it's deprecated in Rails 3. link_to 'link', request.parameters.merge({:per_page => 20}) worked for me.
Vincent
+1  A: 
link_to 'Link', request.parameters.merge({:per_page => 20})
Vincent