如何为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
这个效果很好。但是我也希望在视图中添加一个类别过滤器,以便用户可以选择在特定类别内搜索查询字符串,或者在没有选择类别的情况下搜索所有类别。提前致谢。
您是否尝试过增加一个类别筛选自己吗? –