简体   繁体   English

Ruby On rails 过滤器数量最小值和最大值

[英]Ruby On rails Filter number min and max

Hi I need when I type minimum number and the maximum number to show all the cars in between something like this https://www.lpauto.ca/used-cars-vancouver?Makes=Audi嗨,当我输入最小数字和最大数字时,我需要显示介于以下内容之间的所有汽车https://www.lpauto.ca/used-cars-vancouver?Makes=Audi

Here is my form这是我的表格

<%= form_for "",url: cars_path, role: "search", method: :get do %>
     <%= text_field_tag :searchp, @search_pricen_term,placeholder: "Min..." %>
     <%= text_field_tag :searchpx, @search_pricex_term,placeholder: "Max..." %> 
<% end %>

In the controller in index在索引中的控制器中

if params[:searchp]
  @search_pricen_term = params[:searchp]
  @cars= @cars.search_by(@search_pricen_term)
end

In the model在模型中

def self.search_by(search_pricen_term)
  where("price <= :search_pricen_term OR price >= :search_pricex_term ",
  search_pricen_term: search_pricen_term )
end

Updated the where condition in the model class by using 'scope' which receives both minimum and maximum value and returns all the cars between the given range.通过使用接收最小值和最大值并返回给定范围之间的所有汽车的“范围”更新模型类中的 where 条件。 Hope this helps.希望这可以帮助。

In the controller, pass both minimum and maximum value to the model.在控制器中,将最小值和最大值都传递给模型。

  if params[:searchp] || params[:searchpx] 
    @search_pricen_term = params[:searchp]
    @search_pricex_term = params[:searchpx] 
    @cars = Car.between_range(@search_pricen_term, @search_pricex_term)
  end

In View,在视图中,

    <%= form_for "",url: cars_path, role: "search", method: :get do %>
       <%= text_field_tag :searchp, @search_pricen_term,placeholder: "Min..." %>
       <%= text_field_tag :searchpx, @search_pricex_term,placeholder: "Max..." %> 
       <%= submit_tag "Submit" %>
     <% end %>

In the model,在模型中,

     scope :between_range, -> (min, max) { where("price >= ? OR price <= ?", min, max) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM