0

I am using friendly_id so that I can create such URLs:

/search/france/weekly/toyota-95

My routes:

scope to: 'search#show' do
  get '/search/:car_country_id/:rental_type_id/:car_group_id', as: 'search_country'
end

At the search#show view I have a form:

<%= form_tag search_country_path, :method => :get do %>
    <%= select_tag(:car_country_id, options_from_collection_for_select(CarCountry.all, :slug, proc {|c| c.name }, {:selected => @country}), class: "Select-control u-sizeFull") %>
    <%= submit_tag t('shared.search'), class: 'btn btn-primary' %>
<% end %>

And search controller:

@country = CarCountry.friendly.find(params[:car_country_id])

So ok, my intention is to change the URL as:

/search/italy/weekly/toyota-95

But the thing is, Rails params always sending france as car_country_id when I select country from select tag and submit it.

So what should I do?

unor
  • 92,415
  • 26
  • 211
  • 360
Shalafister's
  • 761
  • 1
  • 7
  • 34

1 Answers1

0

Currently, two car_country_id are sent to Rails server. You can rename one of them:

<%= select_tag(:new_car_country_id, options_from_collection_for_select(CarCountry.all, :slug, proc {|c| c.name }, {:selected => @country}), class: "Select-control u-sizeFull") %>

In your controller, you should check whether new_car_country_id exists. If it does, then redirect to the corresponding path.

Another way is to make sure that the two car_country_id are the same. You should change the form's submit path once select_tag is updated with JavaScript.

unor
  • 92,415
  • 26
  • 211
  • 360
wesley6j
  • 1,393
  • 9
  • 17