列出类别 - 子类别关系到第n级

时间:2014-02-27 05:30:42

标签: ruby-on-rails ruby-on-rails-4 parent-child rails-activerecord

我有一个类别表,其中有一个parent_id列,以下是类别模型

class Category < ActiveRecord::Base

  has_many :subcategories, :class_name => "Category", :foreign_key => :parent_id, :dependent => :destroy
  has_one :parent, :class_name => "Category", :primary_key => :parent_id, :foreign_key => :id
end

我想展示完全动态的父子heirarchy,它可以扩展到第n级。如何使用无序列表在页面上显示它。目前我显示最多3级,但我想让它变得动态。目前我正在使用它来显示最多3个级别,但这种方法不可行,因为它可以扩展到3级以上。

<% @categories.each do |category| %>
        <ul>
          <% if category.parent_id.blank? %>
            <li class="big-bold"><%= category.name %> <%= link_to "Edit", edit_admin_category_path(category), :class => "btn btn-primary btn-xs" %> <%= link_to "Delete", admin_category_path(category), :method => 'delete', :class => "btn btn-danger btn-xs", data: { confirm: 'Are you sure?' } %>
              <% if category.subcategories.present? %>
                <ul>
                  <% category.subcategories.each do |sub| %>
                    <li class="top-clear"><%= sub.name %> <%= link_to "Edit", edit_admin_category_path(sub), :class => "btn btn-primary btn-xs" %> <%= link_to "Delete", admin_category_path(sub), :method => 'delete', :class => "btn btn-danger btn-xs", data: { confirm: 'Are you sure?' } %>
                      <% if sub.subcategories.present? %>
                        <ul>
                          <% sub.subcategories.each do |subtwo| %>
                            <li class="top-clear"><%= subtwo.name %> <%= link_to "Edit", edit_admin_category_path(subtwo), :class => "btn btn-primary btn-xs" %> <%= link_to "Delete", admin_category_path(subtwo), :method => 'delete', :class => "btn btn-danger btn-xs", data: { confirm: 'Are you sure?' } %></li>
                          <% end %>
                        </ul>
                      <% end %>

                    </li>
                  <% end %>
                </ul>
              <% end %>
            </li>
          <% end %>
        </ul>
  <% end %>

数据库查询日志

Started GET "/admin/categories" for 127.0.0.1 at 2014-02-27 12:13:10 +0530
Processing by Admin::CategoriesController#index as HTML
  User Load (0.2ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 14 ORDER BY `users`.`id` ASC LIMIT 1
  UserType Load (0.2ms)  SELECT `user_types`.* FROM `user_types` WHERE `user_types`.`id` = 3 ORDER BY `user_types`.`id` ASC LIMIT 1
  Category Load (0.3ms)  SELECT id, name, parent_id, slug FROM `categories`
  Category Load (0.3ms)  SELECT `categories`.* FROM `categories` WHERE `categories`.`parent_id` IN (8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 33, 34)
  Category Load (0.2ms)  SELECT `categories`.* FROM `categories` WHERE `categories`.`parent_id` = 9
  Category Load (0.2ms)  SELECT `categories`.* FROM `categories` WHERE `categories`.`parent_id` = 10
  Category Load (0.6ms)  SELECT `categories`.* FROM `categories` WHERE `categories`.`parent_id` = 11
  Category Load (0.2ms)  SELECT `categories`.* FROM `categories` WHERE `categories`.`parent_id` = 12
  Category Load (0.2ms)  SELECT `categories`.* FROM `categories` WHERE `categories`.`parent_id` = 13
  Category Load (0.2ms)  SELECT `categories`.* FROM `categories` WHERE `categories`.`parent_id` = 14
  Category Load (0.2ms)  SELECT `categories`.* FROM `categories` WHERE `categories`.`parent_id` = 15
  Rendered admin/categories/_subcategory.html.erb (92.5ms)
  Category Load (0.2ms)  SELECT `categories`.* FROM `categories` WHERE `categories`.`parent_id` = 17
  Category Load (0.2ms)  SELECT `categories`.* FROM `categories` WHERE `categories`.`parent_id` = 18
  Category Load (0.2ms)  SELECT `categories`.* FROM `categories` WHERE `categories`.`parent_id` = 19
  Category Load (0.2ms)  SELECT `categories`.* FROM `categories` WHERE `categories`.`parent_id` = 20
  Category Load (0.2ms)  SELECT `categories`.* FROM `categories` WHERE `categories`.`parent_id` = 21
  Rendered admin/categories/_subcategory.html.erb (7.9ms)
  Category Load (0.2ms)  SELECT `categories`.* FROM `categories` WHERE `categories`.`parent_id` = 24
  Category Load (0.2ms)  SELECT `categories`.* FROM `categories` WHERE `categories`.`parent_id` = 25
  Category Load (0.2ms)  SELECT `categories`.* FROM `categories` WHERE `categories`.`parent_id` = 26
  Category Load (0.2ms)  SELECT `categories`.* FROM `categories` WHERE `categories`.`parent_id` = 27
  Rendered admin/categories/_subcategory.html.erb (5.9ms)
  Category Load (0.2ms)  SELECT `categories`.* FROM `categories` WHERE `categories`.`parent_id` = 28
  Category Load (0.2ms)  SELECT `categories`.* FROM `categories` WHERE `categories`.`parent_id` = 29
  Category Load (0.2ms)  SELECT `categories`.* FROM `categories` WHERE `categories`.`parent_id` = 30
  Category Load (0.2ms)  SELECT `categories`.* FROM `categories` WHERE `categories`.`parent_id` = 33
  Rendered admin/categories/_subcategory.html.erb (8.3ms)
  Rendered admin/categories/index.html.erb within layouts/admin (129.8ms)
  Rendered layouts/admin/_header.html.erb (2.0ms)
  Rendered layouts/admin/_flash_messages.html.erb (0.1ms)
Completed 200 OK in 159ms (Views: 142.3ms | ActiveRecord: 4.9ms)

1 个答案:

答案 0 :(得分:1)

你可以制作一个包含循环标记的部分,例如:

<% @categories.each do |category| %>
        <ul>
          <% if category.parent_id.blank? %>
            <li class="big-bold"><%= category.name %> <%= link_to "Edit", edit_admin_category_path(category), :class => "btn btn-primary btn-xs" %> <%= link_to "Delete", admin_category_path(category), :method => 'delete', :class => "btn btn-danger btn-xs", data: { confirm: 'Are you sure?' } %>
              <% if category.subcategories.present? %>
                <%= render partial: 'subcategory', locals: {subcategories: category.subcategories} %>
              <% end %>
                </ul>
              <% end %>
            </li>
          <% end %>
        </ul>
  <% end %>

并使subcategory部分看起来像这样:

<ul>
  <% subcategories.each do |sub| %>
    <li class="top-clear"><%= sub.name %> <%= link_to "Edit", edit_admin_category_path(sub), :class => "btn btn-primary btn-xs" %> <%= link_to "Delete", admin_category_path(sub), :method => 'delete', :class => "btn btn-danger btn-xs", data: { confirm: 'Are you sure?' } %>
  <% if sub.subcategories.present? %>
    <%= render partial: 'subcategory', locals: {subcategories: sub.subcategories} %>
  <% end %>

</li>
  <% end %>
</ul>