How to add custom filter to Active Admin?

后端 未结 8 2123
眼角桃花
眼角桃花 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:25

    Another way of doing such filtering in newer version of active admin:

    # app/admin/my_model.rb
    filter :my_custom_filter,
    as: :numeric,
    label: 'Custom Filter',
    filters: [:eq]
    

    Then add following 2 functions in your model file

    Your filtering logic:

    def self.my_custom_filter_eq(value)
      where(column_1: value) # or probably a more complex query that uses the value inputted by the admin user
    end
    

    Registering new filter for Ransack

    def self.ransackable_scopes(_auth_object = nil)
      %i(my_custom_filter_eq)
    end
    

提交回复
热议问题