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 ?
If you only need one cgi param and want to stay on the same page, this is very simple to achieve:
However, if you have more than one, you need some logic to keep old ones. It’d probably be best extracted to a helper, but essentially you could do something like this to keep the other params..
Named routes will only really help you here if you need to go to another page.