Rails 5搜索过滤多项选择

时间:2017-10-26 23:30:39

标签: jquery ruby-on-rails ruby search filter

我正在尝试在搜索结果页面上合并搜索过滤器。在用户搜索并在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

但我怎么能:

  1. 使用多个选择过滤器来微调我从搜索表单中获得的搜索结果。
  2. 使用搜索结果填充这些过滤器选项,保留结果并合并参数的新多个值。
  3. 我希望得到类似的内容:

    car gurus

  4. 我不想使用外部依赖项或像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>
    

1 个答案:

答案 0 :(得分:0)

像这样的模块可以工作

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
相关问题