javascriptjqueryruby-on-railsruby-on-rails-5jquery-rails

Rails - How can I modify the http params before the request is made?


I'm doing a car selling website and I applied some filters.

For UX I added the symbol € and some spaces automatically.

But now what I want is when the user presses the submit button, remove the spaces and the symbol before the request is made (in this particular case, the param price_start would be price_start=2500 instead of price_start=2+500+EUR)

enter image description here

I want to do this with Jquery but I don't really know how...I appreciate any help! Thank you :)

This is my html:

<%= form_tag(filter_vehicles_path, method: 'get') do %>
 <h4><strong>

  <span>Fabricante</span>
  <%= collection_select :manufacturer, :manufacturer_id, Manufacturer.joins(:vehicles).uniq, :id, :name, {include_blank: true}, {class: "manufacturer_dropdown"} %>

  <span>Modelo</span>
  <%= grouped_collection_select :model, :model_id, Manufacturer.order(:name), :models, :name,  :id, :name, {include_blank: true}, {class: "model_dropdown", data: {models: @models}} %>

  <span>Preço</span>
  <%= select_tag :price_start, options_for_select([ "250 €", "500 €", "1000 €", "2 000 €", "3 000 €", "4 000 €", "5 000 €", "6 000 €", "7 000 €", "8 000 €", "9 000 €", "10 000 €", "12 000 €", "14 000 €", "16 000 €", "18 000 €", "20 000 €", "22 000 €", "24 000 €", "26 000 €", "28 000 €", "30 000 €", "32 000 €", "34 000 €", "36 000 €", "38 000 €", "40 000 €", "50 000 €", "60 000 €", "70 000 €", "80 000 €", "90 000 €", "100 000 €", "150 000 €", "200 000 €"]), include_blank: true, class:"select2PriceFrom" %>
  <%= select_tag :price_ending, options_for_select([ "250 €", "500 €", "1000 €", "2 000 €", "3 000 €", "4 000 €", "5 000 €", "6 000 €", "7 000 €", "8 000 €", "9 000 €", "10 000 €", "12 000 €", "14 000 €", "16 000 €", "18 000 €", "20 000 €", "22 000 €", "24 000 €", "26 000 €", "28 000 €", "30 000 €", "32 000 €", "34 000 €", "36 000 €", "38 000 €", "40 000 €", "50 000 €", "60 000 €", "70 000 €", "80 000 €", "90 000 €", "100 000 €", "150 000 €", "200 000 €"]), include_blank: true, class:"select2PriceTo" %>

  <span>Ano</span>
  <%= select_tag :year_start, options_for_select((1990..Date.today.year).to_a.reverse + ["1980", "1970", "1960", "1950", "1940", "1930", "1920", "1910", "1900"]), include_blank: true, class:"select2From" %>
  <%= select_tag :year_start, options_for_select((1990..Date.today.year).to_a.reverse + ["1980", "1970", "1960", "1950", "1940", "1930", "1920", "1910", "1900"]), include_blank: true, class:"select2To" %>

  <span>Quilómetros</span>
  <%= select_tag :kms_start, options_for_select([ "5 000 km", "10 000 km", "15 000 km", "20 000 km", "25 000 km", "30 000 km", "35 000 km", "50 000 km", "75 000 km", "100 000 km", "125 000 km", "150 000 km", "200 000 km", "250 000 km", "300 000 km", "350 000 km", "400 000 km", "450 000 km", "500 000 km"]), include_blank: true, class:"select2From" %>
  <%= select_tag :kms_end, options_for_select([ "5 000 km", "10 000 km", "15 000 km", "20 000 km", "25 000 km", "30 000 km", "35 000 km", "50 000 km", "75 000 km", "100 000 km", "125 000 km", "150 000 km", "200 000 km", "250 000 km", "300 000 km", "350 000 km", "400 000 km", "450 000 km", "500 000 km"]), include_blank: true, class:"select2To" %>

  <%= submit_tag "Pesquisar" %>
 </strong></h4>
<% end %>

Solution

  • The options_for_select helper allows you to specify a value different of a formatted output using an array of arrays:

    <%= select_tag :price_start, options_for_select([ ["250 €","250"], ["500 €", "500"], ["1000 €", "1000"] ]) %>
    

    You can even write an helper for that to simplify rewriting lot of entries:

    module ApplicationHelper
    
      def option(value)
        ["#{value} €", "#{value}"]
      end
    end
    
    <%= select_tag :price_start, options_for_select([ option(250), option(500), option(1000) ]) %>