ActiveAdmin -- 如何显示类别分类? (在树类型层次结构中)
我有一个模型 category
,其中包含一个名为 category.parent_id
的字段,用于创建分类法(带有子类别的顶级类别)。
目前,我的索引页面显示的类别为:
Top level category
Sub category
Sub category
Top category
我如何以正确的分类法对它们进行排序(即首先所有顶级类别均取自数据库,然后是子类别等)并显示如下:
Top level category
-- Sub category
-- Sub category
Top category
I have a model category
which contains a field called category.parent_id
which is used to create a taxonomy (top level categories with sub categories).
At the moment my index page shows the categories as:
Top level category
Sub category
Sub category
Top category
How could I order them in a proper taxonomy (i.e. first all top level categories are taken from db then sub categories, etc) and shown as the following:
Top level category
-- Sub category
-- Sub category
Top category
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
经过几个小时的头撞墙后,我想出了这个解决方案:
ActiveAdmin 无法无法渲染整个索引的一部分,但是您可以为单个列渲染一个!
因此,解决方法是为子类别创建一个列,如下所示:
然后在
_children.html.erb
视图部分中,您可以打印出如下列表:After hours of banging my head against the wall, I came up with this solution:
ActiveAdmin can not render a partial for the entire index, however you can render one for a single column!
So the work around was to create a column for sub categories like this:
And then in
_children.html.erb
view partial you can print out the list like this:在您的情况下,您可以创建一个像这样的自引用模型(在
app/model/category.rb
中):然后您可以为父项创建一个范围
,并且在视图中您可以使用这样的迭代
希望这样对你有帮助!
//我读了我想读的内容,...我没有看到它是关于主动管理的。以为是关于 ActiveRecord -.-
You could create a self referencing Model like this in you case (In
app/model/category.rb
):Then you can create a scope for the parents
And in view you can use iteration like this
Hope this helps you!
//I read what I wanted to read,... I didn't see it's about active admin. Thought it's about ActiveRecord -.-