ActiveAdmin:如何添加第二个自定义索引表页面 [英] ActiveAdmin: how to add second custom index table page
问题描述
某些管理员的用户需要具有模型 Bar
的两个表视图:他们已经拥有的默认表视图和另一个具有不同列集的新视图。
设置如下:
ActiveAdmin.register Bar do
#…
索引执行
列:name
列:phone
列:address
end
#…
预计就像添加另一个 index
块一样简单,如下所示:
ActiveAdmin.register Bar执行
#…
索引执行
列:name
列:价格
列:酒保
结束
索引名称:'location'do
列:name
列:phone
列:city
列:国家
结束
然后只需获取其他标签
您可能会猜想它并不是那么简单。 ActiveAdmin现在对虚构的 index name:
属性一无所知,只选择第一个 index
块而无视第二个 index
块。
ActiveAdmin文档显示了一种轻松添加第二/第三/等索引页面,但采用另一种方式:
索引为::grid do | bar |
link_to(image_tag(bar.photo_path),admin_bar_path(bar))
结尾
很好,但是如何添加具有不同列的索引表视图的副本?
有一个窍门。 / p>
如ActiveAdmin的 index
方法之前所示,如:
参数的索引类型编码为符号(ATM,其中之一::block
,:blog
,:grid
和:table
)。除了符号(这只是某些内部AA类的快捷方式)之外,还可以传递任何Ruby类:
index as:CustomTableIndex做
#…
结束
这是解决方案的代码。
新表索引页要做的四件事:
- 创建
ActiveAdmin的子类: Views :: IndexAsTable
- 在子类中用新索引的名称定义类方法
index_name
页面 - 将新类传递给
index
方法 - 为新标签页按钮(如有必要)
在 app / admin / bars.rb
:
ActiveAdmin.register Bar做
#…
# 1.
类MyLocationIndex< ActiveAdmin :: Views :: IndexAsTable
#2.
def self.index_name
bars_location
结束
结束
#3。
索引为:MyLocationIndex do
列:name
列:phone
列:city
列:国家
结束
#…
end
在 config / locales中/admin.yml
:
en:
#…
active_admin:
index_list:
bars_location:位置
Users of some admin need to have two table views of, say, a model Bar
: default one they already have and an additional new one with different set of columns.
The setting is such:
ActiveAdmin.register Bar do
# …
index do
column :name
column :phone
column :address
end
# …
It's expected to be as easy as adding another index
block as in:
ActiveAdmin.register Bar do
# …
index do
column :name
column :price
column :bartender
end
index name: 'location' do
column :name
column :phone
column :city
column :country
end
and then just get the additional tab somewhere.
As you may guess it is not that simple. ActiveAdmin nows nothing about the imaginary index name:
attribute and just selects the first index
block silently ignoring the second index
block.
ActiveAdmin documentation shows a way to add second/third/etc index page with ease but of a different kind:
index as: :grid do |bar|
link_to(image_tag(bar.photo_path), admin_bar_path(bar))
end
Nice, but how to add a duplicate of the index table view with different columns?
There is a trick.
As show before ActiveAdmin's index
method allows the as:
argument with the type of the index coded as symbol (ATM, one of these: :block
, :blog
, :grid
and :table
). Alongside with symbols (which are just shortcuts for some internal AA classes) it's possible to pass any Ruby class:
index as: CustomTableIndex do
# …
end
Here is the code for the solution. Four things to do for our new table index page:
- create a subclass of
ActiveAdmin::Views::IndexAsTable
- define a class method
index_name
in the subclass with a name of the new index page - pass the new class to the
index
method - add a i18n translation for the new tab button (if necessary)
in app/admin/bars.rb
:
ActiveAdmin.register Bar do
# …
# 1.
class MyLocationIndex < ActiveAdmin::Views::IndexAsTable
# 2.
def self.index_name
"bars_location"
end
end
# 3.
index as: MyLocationIndex do
column :name
column :phone
column :city
column :country
end
# …
end
in config/locales/admin.yml
:
en:
# …
active_admin:
index_list:
bars_location: "Locations"
这篇关于ActiveAdmin:如何添加第二个自定义索引表页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!