Rails - 如何在发出请求之前修改 http 参数?
Rails - How can I modify the http params before the request is made?
我正在做一个汽车销售网站,我应用了一些过滤器。
对于用户体验,我自动添加了符号 € 和一些空格。
但现在我想要的是当用户按下提交按钮时,在发出请求之前删除空格和符号(在这种特殊情况下,参数 price_start
将是 price_start=2500
而不是 price_start=2+500+EUR
)
我想用 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 %>
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) ]) %>
我正在做一个汽车销售网站,我应用了一些过滤器。
对于用户体验,我自动添加了符号 € 和一些空格。
但现在我想要的是当用户按下提交按钮时,在发出请求之前删除空格和符号(在这种特殊情况下,参数 price_start
将是 price_start=2500
而不是 price_start=2+500+EUR
)
我想用 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 %>
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) ]) %>