【问题标题】:Rails 5 Search filters multiple choiceRails 5 搜索过滤器多项选择
【发布时间】:2018-04-08 11:40:06
【问题描述】:

我正在尝试在我的搜索结果页面中加入搜索过滤器。在用户搜索并在 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. 我想得到类似的东西:

我不想使用外部依赖项或诸如 ransack 或 filterrific 之类的 gem,我想从头开始学习分面搜索。

如果需要,我的搜索表单代码是:

<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>

【问题讨论】:

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


    【解决方案1】:

    这样的模块可以工作

    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
    

    【讨论】:

    • 好的。但是如何在 sql 中使用像 elasticsearch 这样的聚合/分面,并根据视图中的搜索结果为用户提供过滤器?您通过类​​方法搜索,然后将搜索结果汇总到组中,并在上面的图片中向左侧显示,以及在右侧的此类聚合中显示数字,因此在选择搜索结果时(多个可以也被选中)。
    • 您想在每次选择新项目时重新评估查询?您将不得不使用 Ajax。
    • 另外,我认为您需要针对该主题提出另一个问题。这会使我的答案很长,我不确定我能否充分回答所有问题。
    • 是的。每次用户点击搜索时,他都会获得带有链接的聚合/构面以添加该过滤器。不是静态范围。动态创建的过滤器,如果点击它会为给定的结果添加更多过滤。像变速箱一样:自动(5),手动(4)等,就像上图一样
    • 再次,我认为您应该在单独的问题中提出这个问题。这完全是一个单独的主题,涉及应用程序的其他组件。即JS。我的回答是否满足您当前的问题?
    猜你喜欢
    • 1970-01-01
    • 2014-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-19
    • 2015-12-20
    • 1970-01-01
    • 2022-11-24
    相关资源
    最近更新 更多