2012 年 6 月 29 日更新
我已经设法设置了一个搜索和一个侧栏,用于使用 Sunspot 过滤搜索结果并使用 Rails 充当可标记。我在这里关注本教程, 但我仍然无法一次选择多个过滤器。当我选择一个过滤器时,其他子类别名称仍然消失。我错过了什么?
我原来的问题是这样的:
我不太确定如何一次选择多个方面来过滤数据。所以我有多个子类别,即(远足、滑雪、攀登等)。我希望能够同时选择远足和攀登,因此显示的数据只是远足和攀登的那些对象。现在我选择一个(比方说徒步旅行),所有其他选项都消失了。有人可以解释吗?
下面是我的代码:
齿轮控制器
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
**齿轮索引视图**
<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