How to add custom filter to Active Admin?

后端 未结 8 2140
眼角桃花
眼角桃花 2020-12-25 11:04

Active Admin allows me to define filters that are displayed on the index page like so:

ActiveAdmin.register Promo do

  filter :name
  filter :address
  filt         


        
8条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-25 11:30

    To use a custom filter, you can create a scope function and add it as search_methods in the model.

    For example, on my User model:

    search_methods :role_eq
    scope :role_eq, -> (role) { where("? LIKE ANY(roles)", role) }
    

    Then in users.rb, I can use my scope as a custom filter:

    filter :role, label: "Roles", as: :select, collection: %w[ student teacher parent ]
    

提交回复
热议问题