【发布时间】:2016-09-23 15:09:43
【问题描述】:
在我的分类应用中,我的“主要”模型是Classifieds,但我也实现了分类,所以Categories has_many :classifieds
我还用awesome_nested_set 实现了我的Categories
我也有 Solr 搜索!
在我的网站Classifieds - index.html.erb 中,我呈现分类广告,如果使用搜索,我会呈现相应的分类广告。
现在我已经实现了一个导航栏,它使用Category 模型进行导航。
导航栏通过下拉菜单显示 Category.roots 和每个根的后代。这导致 Category - show.html.erb ,其中 Classifieds 根据它们所在的 Category 显示!
所以问题就在这里:
在我的Classifieds - index.html.erb 中,我成功实现了 Solr facets ,它们用作过滤器,但这仅在用户搜索或仅在初始索引中过滤时才有效!所以简单来说,当他与Classifieds Controller互动时。
当用户使用导航栏时,我如何保留构面功能,这意味着他随后与Category controller进行交互
型号:
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 , :make_country , :condition ,:treecat,:cat,:price #TO BE CHANGED TO CURRENCY FORMAT
time :created_at
string :treecat
string :price #TO BE CHANGED TO CURRENCY FORMAT
string :created_month
string :cat
string :make_country
string :condition
string :make
string :model
end
def cat
category.root.name
end
def treecat
category.name unless category.name == category.root.name
end
def created_month
created_at.strftime("%B")
end
end
class Category < ApplicationRecord
acts_as_nested_set
has_many :classifieds
end
Classifieds controller index action
def index
@search = Classified.search do
paginate(:page => params[:page] || 1, :per_page => 10)
order_by(:created_at , :desc)
fulltext params[:search]
with(:created_at)
active_model = with(:model ,params[:model]) if params[:model].present?
active_make = with(:make , params[:make]) if params[:make].present?
active_make_country = with(:make_country , params[:make_country]) if params[:make_country].present?
active_condition = with(:condition,params[:condition]) if params[:condition].present?
active_category = with(:cat,params[:cat]) if params[:cat].present?
active_subcategory = with(:treecat,params[:treecat]) if params[:treecat].present?
facet(:model)
facet(:make)
facet(:make_country)
facet(:condition)
facet(:cat , exclude: active_subcategory)
facet(:treecat)
end
@classifieds = @search.results
end
类别控制器显示操作
def show
@category = Category.find_by_id(params[:id])
end
分类广告 - index.html.erb
<div class="indexbox">
<div class="col-md-2 col-md-offset-1 visible-lg">
<div class="facets">
<h6>Μάρκα</h6>
<ul class="facetlist">
<% for row in @search.facet(:make).rows %>
<li>
<% if params[:make].blank? %>
<%= link_to(row.value, params.merge(:page => 1,:make => row.value).permit!) %> <small class="rowcount"><%= row.count %></small>
<% else %>
<strong><%= row.value %></strong> (<%= link_to "remove", :make => nil %>)
<% end %>
</li>
<% end %>
</ul>
<h6>Χώρα Κατασκευής</h6>
<ul class="facetlist">
<% for row in @search.facet(:make_country).rows %>
<li>
<% if params[:make_country].blank? %>
<%= link_to(row.value, params.merge(:page => 1,:make_country => row.value).permit!) %> <small class="rowcount"><%= row.count %></small>
<% else %>
<strong><%= row.value %></strong> (<%= link_to "remove", :make_country => nil %>)
<% end %>
</li>
<% end %>
</ul>
<h6>Κατάσταση</h6>
<ul class="facetlist">
<% for row in @search.facet(:condition).rows %>
<li>
<% if params[:condition].blank? %>
<%= link_to(row.value, params.merge(:page => 1,:condition => row.value).permit!) %> <small class="rowcount"><%= row.count %></small>
<% else %>
<strong><%= row.value %></strong> (<%= link_to "remove", :condition => nil %>)
<% end %>
</li>
<% end %>
</ul>
<h6>Κατηγορία</h6>
<ul class="facetlist">
<% for row in @search.facet(:cat).rows %>
<li>
<% if params[:cat].blank? %>
<%= link_to(row.value, params.merge(:page => 1,:cat => row.value).permit!) %> <small class="rowcount"><%= row.count %></small>
<% else %>
<strong><%= row.value %></strong> (<%= link_to "remove", :cat => nil %>)
<% end %>
</li>
<% end %>
</ul>
<h6>Κατηγορία</h6>
<ul class="facetlist">
<% for row in @search.facet(:treecat).rows %>
<li>
<% if params[:treecat].blank? %>
<%= link_to(row.value, params.merge(:page => 1,:treecat => row.value).permit!) %> <small class="rowcount"><%= row.count %></small>
<% else %>
<strong><%= row.value %></strong> (<%= link_to "remove", :treecat => nil %>)
<% end %>
</li>
<% end %>
</ul>
</div>
</div>
<% @classifieds.each do |f| %>
<% if !f.sold %>
<div class="center-div" id="index">
<div class="col-lg-12">
<div class="pull-right">
<div class="listingoptions">
<div class="col-md-3">
<p><%= link_to "", new_classified_message_path(:recipient_id => f.user_id , :classified_id => f.id), :class => "glyphicon glyphicon-envelope" , :style => "color:#EFCE7B" %></p>
<%if current_user.favorite_classifieds.collect(&:classified_id).include?(f.id) %>
<p><%= link_to "", favorite_classified_path(f, type: "unfavorite") , :class => "glyphicon glyphicon-heart" , :style => "color:#FF0000", method: :put %></p>
<%else%>
<p><%= link_to "", favorite_classified_path(f, type: "favorite") , :class => "glyphicon glyphicon-heart-empty" , :style => "color:#000000", method: :put %></p>
<%end%>
<p><%= link_to "", editlisting_path(f) , :class => "glyphicon glyphicon-flag" , :style => "color:#EB573B" %></p>
</div>
</div>
</div>
<%= link_to classified_path(f) , :class => "link" do %>
<div class="media">
<div class="mediabox">
<div class="media-left" href="#">
<!-- <img class="media-object" src="..." alt="Generic placeholder image">-->
<% if f.photos.first %>
<%= image_tag f.photos.first.image.url , :class => "media-object"%>
<%end%>
</div>
<div class="media-body">
<h5 class="media-heading"> <%= f.title %></h5>
<small><%= f.created_month %></small>
<% if f.category.parent_id? %>
<small><%= f.category.root.name %></small>
<%end%>
<small><%= f.category.name %></small>
<div class="price ">
<h5><%= f.price %> </h5>
</div>
</div>
</div>
</div>
</div>
</div>
<%end%>
<%end%>
<%end%>
<div class="center-div">
<div class="digg_pagination">
<%= will_paginate @classifieds , :previous_label => '<', :next_label => '>' %>
</div>
</div>
</div>
类别 show.html.erb
<div class="indexbox">
<div class="center-div" id="index">
<div class="categorytitle">
<h4>
<%= @category.root.name %>
<% unless @category.name == @category.root.name %>
<span> >> </span><%= @category.name %>
</h4>
<%end%>
</div>
</div>
<% @category.self_and_descendants.each do |desc| %>
<% desc.classifieds.each do |f|%>
<% if !f.sold %>
<div class="center-div" id="index">
<div class="col-lg-12">
<div class="pull-right">
<div class="listingoptions">
<div class="col-md-3">
<p><%= link_to "", new_classified_message_path(:recipient_id => f.user_id , :classified_id => f.id), :class => "glyphicon glyphicon-envelope" , :style => "color:#EFCE7B" %></p>
<%if current_user.favorite_classifieds.collect(&:classified_id).include?(f.id) %>
<p><%= link_to "", favorite_classified_path(f, type: "unfavorite") , :class => "glyphicon glyphicon-heart" , :style => "color:#FF0000", method: :put %></p>
<%else%>
<p><%= link_to "", favorite_classified_path(f, type: "favorite") , :class => "glyphicon glyphicon-heart-empty" , :style => "color:#000000", method: :put %></p>
<%end%>
<p><%= link_to "", editlisting_path(f) , :class => "glyphicon glyphicon-flag" , :style => "color:#EB573B" %></p>
</div>
</div>
</div>
<%= link_to classified_path(f) , :class => "link" do %>
<div class="media">
<div class="mediabox">
<div class="media-left" href="#">
<!-- <img class="media-object" src="..." alt="Generic placeholder image">-->
<% if f.photos.first %>
<%= image_tag f.photos.first.image.url , :class => "media-object"%>
<%end%>
</div>
<div class="media-body">
<h5 class="media-heading"> <%= f.title %></h5>
<small><%= f.created_month %></small>
<% if f.category.parent_id? %>
<small><%= f.category.root.name %></small>
<%end%>
<small><%= f.category.name %></small>
<div class="price ">
<h5><%= f.price %> </h5>
</div>
</div>
</div>
</div>
</div>
</div>
<%end%>
<%end%>
<%end%>
<%end%>
</div>
我是菜鸟,我知道我在 html 中的缩进有问题,抱歉!
【问题讨论】:
标签: ruby-on-rails ruby solr facets