Rails - 如何在发出请求之前修改http参数?

时间:2018-06-05 12:02:54

标签: javascript jquery ruby-on-rails ruby-on-rails-5 jquery-rails

我正在开车销售网站,我应用了一些过滤器。

对于UX,我自动添加了符号€和一些空格。

但现在我想要的是当用户按下提交按钮时,在发出请求之前删除空格和符号(在这种特殊情况下,参数price_start将是price_start=2500而不是price_start=2+500+EUR

enter image description here

我想用Jquery做这件事,但我真的不知道如何...我感谢任何帮助!谢谢:))

这是我的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 %>

1 个答案:

答案 0 :(得分:1)

options_for_select帮助器允许您使用数组数组指定不同格式化输出的值:

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

您甚至可以为此编写帮助以简化重写条目的重写:

module ApplicationHelper

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