【问题标题】:How to write filters for methods (not fields) in Active Admin?如何在 Active Admin 中为方法(不是字段)编写过滤器?
【发布时间】:2013-05-13 12:00:15
【问题描述】:

我有一些资源的 Active Admin 索引页面。

ActiveAdmin.register Request do
  index do
    column :created_at
    column :content
    column "Approved", :approved?
    default_actions
  end

  filter :created_at
  filter :content
  filter :approved?
end

问题是它不会为approved? 请求生成过滤器(但它会正确生成列)。我认为这是因为approved 是一种方法,如果approved_at 不是nil,则返回true。在这种情况下如何编写过滤器?

【问题讨论】:

    标签: methods filter customization filtering activeadmin


    【解决方案1】:

    尝试使用范围创建过滤器按钮:

    # model/request.rb
    scope :approved, where("approved_at IS NOT NULL")
    
    # or...if you have other default values on approved_at column you can try this
    scope :approved, where("approved IS NOT", nil)
    scope :approved, where("approved IS NOT ?", "")
    
    #app/admin/requests.rb
    scope :approved
    

    有关 Active Admin 范围的更多信息:http://activeadmin.info/docs/2-resource-customization.html#scoping_the_queries

    【讨论】:

    • def approved? if approved_at then true else false end end
    • 我添加了按钮以通过在approved_at 列上确定范围来过滤已批准。请尝试:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-31
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多