ActiveAdmin -- 如何显示类别分类? (在树类型层次结构中)

发布于 2024-12-15 00:30:53 字数 358 浏览 3 评论 0原文

我有一个模型 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧人九事 2024-12-22 00:30:53

经过几个小时的头撞墙后,我想出了这个解决方案:

ActiveAdmin 无法无法渲染整个索引的一部分,但是您可以为单个列渲染一个!

因此,解决方法是为子类别创建一个列,如下所示:

    column "Sub Categories" do |category|
      children = Category.children(category.id)
      if children.present?
        render :partial => "children", :locals => { :children => children }
      end
    end

然后在 _children.html.erb 视图部分中,您可以打印出如下列表:

    <% children.each do |child| %>
        <p><%= link_to child.name, edit_admin_category_path(child) %></p>
    <% end %>

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:

    column "Sub Categories" do |category|
      children = Category.children(category.id)
      if children.present?
        render :partial => "children", :locals => { :children => children }
      end
    end

And then in _children.html.erb view partial you can print out the list like this:

    <% children.each do |child| %>
        <p><%= link_to child.name, edit_admin_category_path(child) %></p>
    <% end %>
鸢与 2024-12-22 00:30:53

在您的情况下,您可以创建一个像这样的自引用模型(在 app/model/category.rb 中):

belongs_to :parent, :class_name => 'Category', :foreign_key => 'parent_id'
has_many :children, :class_name => 'Category', :foreign_key => 'parent_id'

然后您可以为父项创建一个范围

scope :parents, where("parent_id IS NULL")

,并且在视图中您可以使用这样的迭代

<ul>
<% Category.all.parents.each do |parent| %>
     <li class="parent"><%= link_to parent.name, parent %></li>
     <% parent.children.each do |child| %>
          <li class="sub"><%= link_to child.name, child %></li>
     <% end %>
<% end %>
</ul>

希望这样对你有帮助!

//我读了我想读的内容,...我没有看到它是关于主动管理的。以为是关于 ActiveRecord -.-

You could create a self referencing Model like this in you case (In app/model/category.rb):

belongs_to :parent, :class_name => 'Category', :foreign_key => 'parent_id'
has_many :children, :class_name => 'Category', :foreign_key => 'parent_id'

Then you can create a scope for the parents

scope :parents, where("parent_id IS NULL")

And in view you can use iteration like this

<ul>
<% Category.all.parents.each do |parent| %>
     <li class="parent"><%= link_to parent.name, parent %></li>
     <% parent.children.each do |child| %>
          <li class="sub"><%= link_to child.name, child %></li>
     <% end %>
<% end %>
</ul>

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 -.-

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文