Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add parameters to current URL in rails [duplicate]

I have a logic where I allow sorting on price and relevance. I am doing this by passing parameters to controller. My URL has a parameter - 'sort' which can have a value - 'price_lowest' or 'default'. The links looks like:

<a href="<%= request.fullpath + '&sort=price_lowest' %>">lowest prices</a> |  <a href="<%= request.fullpath + '&sort=default' %>">relevance</a> 

problem with the above code is that it "adds" parameters and does not "replace" them. I want to replace the value of &sort= parameter without adding a new value. E.g. I don't want :

../&sort=price_lowest&sort=price_lowest&sort=default 

With the current logic - I am getting the above behaviour. Any suggestions ?

like image 896
Ved Avatar asked Jul 08 '11 13:07

Ved


1 Answers

In order to preserve the params I did this:

<%= link_to 'Date', params.merge(sort: "end_date") %> 

However the url will be ugly.

UPDATE

For Rails 5 use:

<%= link_to 'Date', request.params.merge(sort: "end_date") %> 
like image 165
lulalala Avatar answered Oct 02 '22 20:10

lulalala