如何爲searchkick實現類別過濾器。基本上我有一個輸入字段接受查詢字詞,但我也想要一個下拉菜單,用戶可以選擇一個類別來搜索或搜索所有類別。有職位和類別Rails 4與多對多關係的searchkick
我的模型之間的許多一對多的關係是:
-- post.rb
class Post < ActiveRecord::Base
has_many :post_categories
has_many :categories, through: :post_categories
searchkick text_start: [:title]
end
-- category.rb
class Category < ActiveRecord::Base
has_many :post_categories
has_many :posts, through: :post_categories
end
--post_category.rb
class PostCategory < ActiveRecord::Base
belongs_to :post
belongs_to :category
end
我posts_controller index動作
現在,我有以下的,其通過返回匹配的所有帖子到目前爲止作品查詢參數,或者如果搜索輸入中未提供查詢參數,則返回所有帖子。
class PostsController < ApplicationController
def index
query = params[:q].presence || "*"
@posts = Post.search (query)
end
end
這個效果很好。但是我也希望在視圖中添加一個類別過濾器,以便用戶可以選擇在特定類別內搜索查詢字符串,或者在沒有選擇類別的情況下搜索所有類別。提前致謝。
您是否嘗試過增加一個類別篩選自己嗎? –