简体   繁体   中英

How to Customize the Filter in activeadmin

I want to customize my filter in activeadmin panel for applications panel. currently i have a filter like this. 目前我有这样的过滤器

and want the filter like this

在此处输入图片说明

i do the code for this as follow

For add category element in filter i do as follow code.

There is 3 tables called

  1. applications
  2. categories
  3. application_categories

and i want the filter data from application_categories as per selection on category from filter.

Structure of tables as follow.

  1. applications table :

    1. id
    2. name
    3. status
    4. developer_id
    5. version
    6. language_id ,etc...
  2. categories table

    1. id
    2. name
    3. parent_id
  3. application_categories table

    1. id
    2. application_id
    3. category_id

In activeadmin resource file
app/admin/applications.rb file code

filter :status, :as => :select, :collection => ["pending", "approved", "declined"]
filter :featured, :as => :select
filter :developer, :collection => proc {(Developer.all).map{|d| [d.company_name, d.id]}}
filter :category , :collection => proc {(Category.all).map{|c| [c.name,c.id]}}
filter :name
filter :version
filter :tags

In application model file
in model app/models/application.rb

belongs_to :developer
belongs_to :bundle
belongs_to :license
belongs_to :language
belongs_to :category

has_many :application_categories, :dependent => :destroy
has_many :categories, :through => :application_categories

It's display category element in filter panel, but when we select any category and click on filter it's generate an error as shown below :

Mysql2::Error: Unknown column 'applications.category_id' in 'on clause': SELECT COUNT(DISTINCT count_column) FROM (SELECT applications . id AS count_column FROM applications LEFT OUTER JOIN categories ON categories . id = applications . category_id WHERE categories . id = 32 LIMIT 30 OFFSET 0) subquery_for_count

In app/admin/applications.rb :

filter :categories

In app/models/application.rb : delete belongs_to :category

If it doesn't work, you have to update your ActiveAdmin gem: bundle update

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM