简体   繁体   English

如何在activeadmin中自定义过滤器

[英]How to Customize the Filter in activeadmin

I want to customize my filter in activeadmin panel for applications panel. 我想在activeadmin面板中为应用程序面板自定义过滤器。 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 有3张桌子叫

  1. applications 应用领域
  2. categories 分类
  3. application_categories application_categories

and i want the filter data from application_categories as per selection on category from filter. 而且我希望根据对过滤器类别的选择,从application_categories中获取过滤器数据。

Structure of tables as follow. 表的结构如下。

  1. applications table : 申请表:

    1. id ID
    2. name 名称
    3. status 状态
    4. developer_id developer_id
    5. version
    6. language_id ,etc... language_id等
  2. categories table 类别表

    1. id ID
    2. name 名称
    3. parent_id parent_id
  3. application_categories table application_categories表

    1. id ID
    2. application_id application_id
    3. category_id category_id

In activeadmin resource file 在activeadmin资源文件中
app/admin/applications.rb file code app / admin / applications.rb文件代码

filter :status, :as => :select, :collection => ["pending", "approved", "declined"] 过滤器:status,:as =>:select,:collection => [“待处理”,“已批准”,“已拒绝”]
filter :featured, :as => :select 过滤器:featured,:as =>:select
filter :developer, :collection => proc {(Developer.all).map{|d| 过滤器:developer,:collection => proc {(Developer.all).map {| d | [d.company_name, d.id]}} [d.company_name,d.id]}}
filter :category , :collection => proc {(Category.all).map{|c| 过滤器:category,:collection => proc {(Category.all).map {| c | [c.name,c.id]}} [c.name,c.id]}}
filter :name 过滤器:名称
filter :version 过滤器:version
filter :tags 过滤器:标签

In application model file 在应用程序模型文件中
in model app/models/application.rb 在模型app / models / application.rb中

belongs_to :developer 当属:开发者
belongs_to :bundle 归属于:bundle
belongs_to :license 所属:许可证
belongs_to :language 归属于:language
belongs_to :category 相关类:类别

has_many :application_categories, :dependent => :destroy has_many:application_categories,:depend =>:destroy
has_many :categories, :through => :application_categories 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 Mysql2 ::错误:未知柱在'applications.category_id' 'on子句':SELECT COUNT(DISTINCT count_column)FROM(SELECT applicationsid AS count_column FROM applications LEFT OUTER JOIN categories ON categoriesid = applicationscategory_id WHERE categoriesid = 32 LIMIT 30 OFFSET 0)subquery_for_count

In app/admin/applications.rb : app/admin/applications.rb

filter :categories

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

If it doesn't work, you have to update your ActiveAdmin gem: bundle update 如果它不起作用,则必须更新ActiveAdmin gem: bundle update

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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