2016-09-23 68 views
0

在我的分類應用程序之間SOLR方面,我的「主」模式是Classifieds,但我還實施了分類,以便Categories has_many :classifiedsRuby on Rails的2款

此外,我已經實現了我的Categoriesawesome_nested_set

,我有Solr搜索也!

在我的網站Classifieds - index.html.erb我渲染分類廣告,如果使用搜索我提供相應的分類廣告。

現在我已經實現了一個導航欄,它使用Category模型進行導航。 導航欄使用下拉菜單顯示Category.roots和每個根的後代。這導致Category - show.html.erb,其中Classifieds顯示根據他們在Category

所以問題是在這裏: 在我的Classifieds - index.html.erb我已經成功實施了Solr方面,它被用作過濾器,但只有當用戶搜索或只在初始索引中過濾時才起作用!所以簡單地說,當他與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 

分類控制器show動作

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> 

IM一個小白,我知道有東西錯了我的縮進HTML,對不起那個!

回答

0

好吧,我通過嵌套路線並從類別模型中獲取了一些字段。

resources :categories do 
     resources :classifieds do 
     end 
    end 

分類模型:

def nested_classifieds 
    Classified.where(category_id: self_and_descendants.select(:id)) 
end 

def nested_categories 
    self_and_descendants  
end 

公告控制器

def index 
    if @category.present? 
     @classifieds = @category.nested_classifieds 
     nested_categories = [] 
     @category.nested_categories.each do |f| 
     nested_categories << f.id 
     end 

     @search = Sunspot.search(Classified) do 
     paginate(:page => params[:page] || 1, :per_page => 10) 
     order_by(:created_at , :desc) 
     fulltext params[:search]  
     with(:categoryid,nested_categories) 
     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? 
     active_pricerange = with(:price, params[:price]) if params[:price].present? 

     fulltext params[:prc] 
     facet :price 
     with(:price).less_than(1000) 

     facet(:model) 
     #facet(:model , exclude: active_condition)  
     facet(:make)  
     facet(:make_country)  
     facet(:condition) 
     facet(:cat) 
     facet(:treecat)  
     end 
     @classifieds = @search.results 
     else 
      #redirect_to '/' 
      #@[email protected] 
      search 
    end 
    end