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 ?
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") %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With