[英]Rails 5 Search filters multiple choice
我正在嘗試將搜索過濾器合並到我的搜索結果頁面上。 用戶搜索后,結果顯示在search.html.erb上,我希望他們使用過濾器。 過濾器將從搜索結果本身派生。
換句話說,我想復制汽車專家的工作。 您使用品牌,型號,價格進行搜索,然后過濾器將為您提供多面搜索的選項,具體取決於搜索車輛的內飾,變速箱等。
我嘗試了單個過濾器鏈接,例如:
<%= addfilters "transmission", "Automatic" %>
並定義諸如
def addfilters(column, title)
link_to title, params.permit(:NewUsed, :category, :subcategory, :minprice, :maxprice, :location, :radius).merge({:"#{column}" => "Automatic"})
end
但是我該如何:
我不想使用外部依賴項或ransack或filterrific之類的寶石,我想從頭開始學習多面搜索。
如果需要,我的搜索表單代碼為:
<div id="Make" class="tabcontent1">
<section class="formclass">
<!-- f.select :transmission, ['Automanual','Automatic','Automatic 4 Speed','Automatic 5 Speed','Automatic 6 Speed','CVT','Manual'] -->
<h3 style = "color:#F00000; text-align: center;"><strong><%= @carcount %> CARS LISTED!</strong></h3>
<hr>
<h3 style = "color:#7C064D;"><span class="glyphicon glyphicon-search"></span> <strong>SEARCH CARS FOR SALE BY MAKE</strong></h3>
<%= form_tag search_listings_path, method: :get, class: 'navbar-form navbar-center' do |f| %>
<div class= "col-xs-12 col-sm-12 col-lg-12 col-md-12">
<%= select_tag :NewUsed, "<option>New</option><option>Used</option>".html_safe, style: "width: 100%; margin: 1% 0;" %>
</div>
<div class= "col-xs-6 col-sm-6 col-lg-6 col-md-6">
<%= select_tag :category, include_blank: true, style: "width: 100%; margin: 1% 0;" %>
</div>
<div class= "col-xs-6 col-sm-6 col-lg-6 col-md-6">
<%= select_tag :subcategory, include_blank: true, style: "width: 100%; margin: 1% 0;" %>
</div>
<!-- <div class= "col-xs-12 col-sm-12 col-lg-12 col-md-12"> -->
<div class= "col-xs-6 col-sm-6 col-lg-6 col-md-6">
<%= text_field_tag :minprice, nil, placeholder: 'Min Price', style: "width: 100%; margin: 1% 0;" %>
</div>
<div class= "col-xs-6 col-sm-6 col-lg-6 col-md-6">
<%= text_field_tag :maxprice, nil, placeholder: 'Max Price', style: "width: 100%; margin: 1% 0;" %>
</div>
<!-- </div> -->
<div class= "col-xs-6 col-sm-6 col-lg-6 col-md-6">
<%= text_field_tag :location, nil, placeholder: 'Near', style: "width: 100%; margin: 1% 0;" %>
</div>
<div class= "col-xs-6 col-sm-6 col-lg-6 col-md-6">
<%= text_field_tag :radius, nil, placeholder: 'Radius', style: "width: 100%; margin: 1% 0;" %>
</div>
<div class= "col-xs-12 col-sm-12 col-lg-12 col-md-12">
<%= submit_tag 'Search', class: 'btn btn-danger', style: "width: 100%;" %>
</div>
<% end %>
</section>
</div>
這樣的模塊可以工作
module Filterable
extend ActiveSupport::Concern
module ClassMethods
def filter(filtering_params)
results = self.where(nil)
filtering_params.each do |key, value|
results = results.public_send(key, value) if value.present?
end
results
end
end
end
#Controller
def index
@products = Product.filter(params.slice(:status, :location, :over_price, :under_price))
end
#class Car < ApplicationRecord
class Product < ApplicationRecord
include Filterable
scope :status, -> (status) { where status: status }
scope :location, -> (location_id) { where location_id: location_id }
scope :over_price, -> (price) { where "price > ?", price) }
scope :under_price, -> (price) { where "price < ?", price) }
#continue adding more scopes here
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.