-1
我試圖以一種方式實現方面,當您選擇一個時,所有其他人都不會被取消,因此我可以同時擁有2個方面。Ruby on Rails Solr - 多個方面
我的網站有公告因此,例如,如果我有一個公告"Fender Bass"
,一個公告"Fender Stratocaster"
和分類"Gibson Bass"
如果我選擇的面Bass
IM能夠看到兩個低音但後來我想更深的篩選,現在選擇Fender
如果我這樣做,生病得到"Fender Stratocaster"
和"Fender Bass"
和Bass
方面將不活動,我打算做的是後Bass
我的模式選擇Fender
當從Bass
進入更深只"Fender Bass"
:
create_table "classifieds", force: :cascade do |t|
t.string "make"
t.string "model"
t.string "year"
t.string "color"
t.string "title"
t.string "condition"
t.string "price"
t.string "offer"
t.string "make_country"
t.text "description"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "category_id"
t.index ["category_id"], name: "index_classifieds_on_category_id"
t.index ["user_id"], name: "index_classifieds_on_user_id"
end
我的模型:
class Classified < ApplicationRecord
belongs_to :user
belongs_to :category
has_many :photos, dependent: :destroy, inverse_of: :classified
has_many :favorite_classifieds, dependent: :destroy
has_many :favorited_by , through: :favorite_classifieds , source: :user #for the favorite_by to work :source is needed
accepts_nested_attributes_for :photos
searchable do
text :title, :boost => 5
text :model , :created_month , :make
time :created_at
string :created_month
string :make
string :model
end
def created_month
created_at.strftime("%B")
end
end
我的控制器:
def index
@search = Classified.search do
fulltext params[:search]
with(:created_at)
facet :model
with(:model , params[:model]) if params[:model].present?
facet :make
with(:make , params[:make]) if params[:make].present?
facet :created_month
with(:created_month , params[:month]) if params[:month].present?
end
@classified = @search.results
end
View.html.erb
<div class="col-md-9 visible-lg">
<div class="facets">
<h6>Μοντέλο</h6>
<ul>
<% for row in @search.facet(:model).rows %>
<li>
<% if params[:model].blank? %>
<%= link_to row.value, :model => row.value %> (<%= row.count %>)
<% else %>
<strong><%= row.value %></strong> (<%= link_to "remove", :model => nil %>)
<% end %>
</li>
<% end %>
</ul>
<h6>Μάρκα</h6>
<ul>
<% for row in @search.facet(:make).rows %>
<li>
<% if params[:make].blank? %>
<%= link_to row.value, :make => row.value %> (<%= row.count %>)
<% else %>
<strong><%= row.value %></strong> (<%= link_to "remove", :make => nil %>)
<% end %>
</li>
<% end %>
</ul>
</div>
</div>
任何幫助,不勝感激!