user3477051 user3477051 - 2 months ago 24
Ruby Question

adding second parameter to URL in rails

I am trying to add a second parameter to the URL (and still keep the first parameter). eg I have the url /shops?sorting=alphabetically, and want to add the parameter currency=CAD

%form{action: shops_path(params.merge!({currency: params[:currency]}))}
.input-group
= select_tag 'currency', options_for_select({'USA' => 'USD' , 'Canada' => 'CAD'}, params[:currency]), class: 'select2'


In the code above, however, the parameter just gets replaced, eg I get /shops?currency=CAD instead of /shops?sorting=alphabetically&currency=CAD
What am I doing wrong?

RSB RSB
Answer

Use hidden_field_tag instead,

= form_tag shops_path do
  .input-group
    = select_tag 'currency', options_for_select({'USA' => 'USD' , 'Canada' => 'CAD'}, params[:currency]), class: 'select2'
    = hidden_field_tag :sorting, params[:sorting] 

Hope that helps!