在 Active Admin 中自定义索引
Customizing Index in Active Admin
我想自定义索引页,比如根据id显示详情。例如,我有两个表,如商家和客户
merchant.rb
class Merchant < ActiveRecord::Base
has_many :customers
end
customer.rb
class Customer < ActiveRecord::Base
belongs_to :merchant
end
目前我的代码是这样的
app/admin/merchant.rb
index do
selectable_column
column :merchant_id
column :name
actions
end
它显示商家 ID 和商家名称,点击 merchant_id 它应该显示相关客户。给出一些实现思路
在一行中显示每个商家的所有客户是不切实际的,所以通常我更愿意显示客户数量,然后 link 将其添加到带有商家过滤器集的客户索引中。
ActiveAdmin.register Merchant do
def scoped_collection
super.select('merchants.*, COUNT(*) AS customer_count')
.joins(:customers)
.group('merchants.id')
end
index do
selectable_column
id_column
column :customer_count, sortable: 'customer_count' do |merchant|
link_to merchant.attributes['customer_count'],
admin_customers_path(q: { merchant_id_eq: merchant.id })
end
actions
end
end
我想自定义索引页,比如根据id显示详情。例如,我有两个表,如商家和客户
merchant.rb
class Merchant < ActiveRecord::Base
has_many :customers
end
customer.rb
class Customer < ActiveRecord::Base
belongs_to :merchant
end
目前我的代码是这样的
app/admin/merchant.rb
index do
selectable_column
column :merchant_id
column :name
actions
end
它显示商家 ID 和商家名称,点击 merchant_id 它应该显示相关客户。给出一些实现思路
在一行中显示每个商家的所有客户是不切实际的,所以通常我更愿意显示客户数量,然后 link 将其添加到带有商家过滤器集的客户索引中。
ActiveAdmin.register Merchant do
def scoped_collection
super.select('merchants.*, COUNT(*) AS customer_count')
.joins(:customers)
.group('merchants.id')
end
index do
selectable_column
id_column
column :customer_count, sortable: 'customer_count' do |merchant|
link_to merchant.attributes['customer_count'],
admin_customers_path(q: { merchant_id_eq: merchant.id })
end
actions
end
end