主动管理过滤器

Pav*_*vel 2 ruby ruby-on-rails activeadmin

我有一个模型page.rb和模型comment.rb和评论belongs_to页面.我也在使用gem active admin.在索引页面上,我有一个由"过滤器"制作的搜索表单,我有

  filter :country
  filter :city
  filter :category
  filter :description_type
  filter :title
  filter :sight_of_the_day, :as => :select
  filter :active, :as => :select
  filter :show_in_top, :as => :select
  filter :created_at
  filter :updated_at
Run Code Online (Sandbox Code Playgroud)

它工作正常,但我想再增加一个过滤器.我想创建一个名为"comments"的复选框,如果选中它,我想查找只有评论的页面.我如何在主动管理员中执行此操作?提前致谢!

Iva*_*van 10

也许你想要这样的东西:

filter :has_comments, :as => :select
Run Code Online (Sandbox Code Playgroud)

这将生成带有"Any","Yes","No"选项的select.您还需要搜索方法"has_comments_eq":

scope :has_comments_eq, lambda { |has| has == "true" ? with_comments : without_comments }
search_method :has_comments_eq
Run Code Online (Sandbox Code Playgroud)

search_method doc

  • 在最新的ActiveAdmin中,MetaSearch已被Ransack取代,因此您需要使用`ransacker`而不是`search_method`.这是一个[例子](https://github.com/ernie/ransack/blob/master/spec/support/schema.rb#L18). (3认同)