gpt4 book ai didi

ruby-on-rails - 选择多个方面或同时过滤数据

转载 作者:行者123 更新时间:2023-12-04 06:13:06 25 4
gpt4 key购买 nike

12 年 6 月 29 日更新

我设法设置了一个搜索和一个侧栏,用于使用 Sunspot 过滤搜索结果并使用 Rails 作为可标记。我正在学习本教程 here
但我仍然无法一次选择多个过滤器。当我选择过滤器时,其他子类别名称仍然消失。我错过了什么?

我原来的问题是这样的:

I'm not quite sure how to select multiple facets at once filtering the data. So I have multiple sub categories i.e.(hiking, skiing, climbing, etc.) I want to be able to select hiking and climbing simultaneously so the data shown is only those objects of hiking and climbing. Right now I select one (let's say hiking) and all the other options disappear. Can someone explain?



下面是我的代码:

齿轮 Controller
  def index
@search = Gear.solr_search do
exclusions = []
fulltext params[:search]
exclusions << with(:sub_category_name, params[:name]) if params[:name].present?
exclusions.compact!
exclusions = nil if exclusions.empty?
facet :sub_category_name
facet :sub_category_name, :exclude => exclusions, :name => :all_categories
paginate(page: params[:page], :per_page => 15)
end

@gears = @search.results
end

** 齿轮索引 View **
<div class="gears_container">
<div class="side_bar_search">
<%= form_tag gears_path, :method => :get do %>
<p>
<%= text_field_tag :keywords, params[:keywords] , class: 'gearsearchbar' %> <%= submit_tag "Search", :name => nil, class: 'btn btn-inverse gearsearchbutton' %>

</p>
<% end %>

<div class="sidebar_section">Sub Category</div>
<ul>
<% for row in @search.facet(:all_categories).rows %>
<li class="sidebar_options">
<% if params[:name].present? %>
<strong><%= row.value %></strong>(<%= link_to "remove", :name => nil %>)
<% else %>
<%= link_to row.value, :name => row.value %> (<%= row.count %>)
<% end %>
</li>
<% end %>
</ul>
</div>
<div c
<div class="search_results_gear">
<% @hits.each do |gear| %>
<%= render partial: 'gear', locals: {gear: gear} %>
<% end %>
<div style="clear:both"></div>
<%= will_paginate @hits, class: 'flickr_pagination' %>
</br>
</div>
</div>

齿轮型号
class Gear < ActiveRecord::Base
attr_accessible :title, :size, :price, :sub_category_id, :user_id, :image, :image_a, :remote_image_url, :color, :year, :latefee, :cancellation, :minrental, :policy, :about, :address, :city, :state, :zip, :sub_category_name
belongs_to :user
belongs_to :sub_category
has_one :category, :through => :sub_category
has_many :comments, :dependent => :destroy
has_many :line_items
require 'carrierwave/orm/activerecord'
mount_uploader :image, GearpicUploader
mount_uploader :image_a, GearpicUploader
before_destroy :ensure_not_referenced_by_any_line_item
acts_as_taggable_on :tags

validates :title, presence: true
validates :size, presence: true
validates :price, presence: true
validates :sub_category_id, presence: true
validates :user_id, presence: true

searchable do
text :title, :size, :price, :year, :zip, :state, :city, :minrental, :about, :latefee, :color

text :user_firstname do
user.firstname
end

text :user_lastname do
user.lastname
end
# **Facet Section**

string :size, :price, :year, :zip, :state, :city, :minrental, :latefee, :color

string :sub_category_name , :multiple => true, :stored => true do
sub_category.name
end

string :category_name do
category.name
end
end

private
def ensure_not_referenced_by_any_line_item
if line_items.empty?
return true
else
errors.add(:base, 'Line Items present')
return false
end
end

end

最佳答案

看来您需要利用 :exclude分面搜索的选项。

def index
@search = Gear.search do
exclusions = []
# tags, AND'd
if params[:tag].present?
all_of do
params[:tag].each do |tag|
exclusions << with(:sub_category_name, tag)
end
end
end
exclusions.compact!
exclusions = nil if exclusions.empty?
facet :sub_category_name
facet :sub_category_name, :exclude => exclusions, :name => :all_categories
paginate(page: params[:page])
end
@hits = @search.results
end

所以我在上面所做的是创建一个 exclusions用于保存过滤器集合的数组。 with()方法返回一个过滤器对象,因此我们捕获 exclusions 中的所有过滤器大批。我们做 compact!从数组中删除 nil 个对象,如果它是空的,我们让它 nil (这是由于 :exclude 选项的限制,如果传递给它一个空数组,它将无法正常工作)。然后我们 facet同上 :sub_category_name术语,不包括过滤器,并为其命名。此 facet将为您提供除选定类别之外的所有构面类别。

然后在您的 View 中,当您显示构面选项时,您将使用命名 :all_categories方面。
@search.facet(:all_categories).rows.each_with_index do |facet, index|
# This just outputs all the facets, you can add the logic in.
<li><%= facet.value %> (<%= facet.count %>)</li>
end

关于ruby-on-rails - 选择多个方面或同时过滤数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10560583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com