2016-09-20 10 views
-1

ファセットを選択すると他のすべてがキャンセルされないので、同時に2つのファセットを実装できるようにしようとしています。例えば、私が"Fender Bass"分類している場合のでRuby on Rails Solr - 複数のファセット

私のサイトは"Fender Stratocaster"を分類し、私は両方の低音を見ることができ、ファセットBassイムを選択した場合"Gibson Bass"を分類したが、その後、私は深くフィルタリングして、今Fenderを選択したいのですが、分類があります私はそれをすれば"Fender Stratocaster""Fender Bass"を取得し、Bassファセットがアクティブになりません、病気、私は何をするつもりはFender

Bass後の私のスキーマを選ぶときにのみ "Fender Bass"から 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の複数のファセットで機能します! DEFインデックス

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

何が起こるかであるあなたが最後に.permit!を使用する必要はありませattr_accessorことができ、レール5、中作業のためにマージの!

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

私は、これは他の誰かに役立ちます願っています:)