2016-09-20 71 views
-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> 

任何幫助,不勝感激!

回答

0

好吧,我結束了這一點,這適用於軌道5中的多個方面!

控制器: 高清指數

@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? 

     facet(:model) 



     facet(:make)    


     facet(:make_country) 

     facet(:condition) 

     facet(:cat) 


     facet :created_month 
     with(:created_month , params[:month]) if params[:month].present? 

    end 
    @classifieds = @search.results 

end 

,並在視圖:

<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> 

     </div> 

發生的事情是,在導軌5,在那裏你可以不attr_accessor你必須在年底使用.permit!爲了工作而合併!

<%= link_to(row.value, params.merge(:page => 1,:cat => row.value).permit!) %> 

我希望這可以幫助別人:)