类别和子类别模型轨道 [英] categories and sub-categories model rails

查看:73
本文介绍了类别和子类别模型轨道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在不使用任何宝石的情况下,我该如何在铁轨上这样做?

Without using any gems how do I do this in rails?

主要类别
子类别
子类别
子类别

Main Category
 Sub Category
 Sub Category
 Sub Category

主要类别
子类别
子类别
子类别

Main Category
 Sub Category
 Sub Category
 Sub Category

主要类别
子类别
子类别
子类别

Main Category
 Sub Category
 Sub Category
 Sub Category

我有一个包含|的表. id | 1级| level2 |

I have a table that consists of | id | level1 | level2 |

第1级是主要类别,第2级是子类别

Level 1 being the main category and Level 2 being the subcategory

我希望它显示在上面的视图中.

I would like it displayed in the view like above.

在Internet上四处逛逛之后,每个人似乎都建议使用像树上的宝石一样的行为,但是我想避免使用它们,因为我是Rails的新手,我想了解如何做事,而不是去做.转向宝石.

After looking around on the internet everyone seems to recommend using acts-like-a-tree gem, but i want to avoid using them as I'm fairly new to rails and I would like to understand how to do things rather than turn to gems.

您的帮助非常重要

型号:

class Category < ActiveRecord::Base
belongs_to :catalogue
    has_many :subcategories, :class_name => "Category", :foreign_key => "parent_id", :dependent => :destroy
belongs_to :parent_category, :class_name => "Category"
end

控制器:

class CataloguesController < ApplicationController
  layout 'main'
  def index
   @cats = Catalogue.all
  end

  def categories
   @cat = Catalogue.find(params[:id])
  end

end

查看:

<ul class="unstyled-list">

    <% @cat.categories.order([:level1]).each do |cat|%>
        <li><%=  cat.level1 %></li>
        <li><%= cat.level2 %></li>
    <% end %>
</ul>

推荐答案

创建一个对子类别(或子子类别等)具有自身引用的模型:

Create a model that has references to itself for a sub-category (or a sub-sub-category, etc):

class Category < ActiveRecord::Base
  has_many :subcategories, :class_name => "Category", :foreign_key => "parent_id", :dependent => :destroy
  belongs_to :parent_category, :class_name => "Category"
end

  • has_many定义模型类型为Categorysubcategories关联.即它使用相同的表.
  • belongs_to定义了与父类别的关系(可选,不是必需的)
    • the has_many defines a subcategories association of the model type Category. Ie it uses the same table.
    • the belongs_to defines a relation back to the parent category (optional, not required)
    • 有关has_manybelongs_to的模型关联的更多信息,请阅读关联基础指南.

      For more information on model associations, has_many or belongs_to, read the Associations Basics Guide.

      要创建表,请使用以下迁移:

      To create the table use this migration:

      class CreateCategories < ActiveRecord::Migration
        def self.up
          create_table :category do |t|
            t.string      :text
            t.references  :parent
            t.timestamps
          end
        end
      end
      

      注意:此表格式与您建议的表格式(略有不同),但是我想这不是一个真正的问题.

      Note: this table format is (slightly) different than you proposed, but I suppose that this is not a real problem.

      迁移指南包含有关数据库迁移的更多信息.

      The Migrations Guide contains more information on database migrations.

      在您的控制器中使用

      def index
        @category = nil
        @categories = Category.find(:all, :conditions => {:parent_id => nil } )
      end
      

      查找没有父级的所有类别,即主要类别

      to find all categories without a parent, ie the main categories

      要查找任何给定类别的所有子类别,请使用:

      To find all sub-categories of any given category use:

      # Show subcategory
      def show
        # Find the category belonging to the given id
        @category = Category.find(params[:id])
        # Grab all sub-categories
        @categories = @category.subcategories
        # We want to reuse the index renderer:
        render :action => :index
      end
      

      要添加新类别,请使用:

      To add a new category use:

      def new
        @category = Category.new
        @category.parent = Category.find(params[:id]) unless params[:id].nil?
      end 
      

      它会创建一个新类别并设置父类别(如果有的话)(否则它将成为主类别)

      It creates a new category and sets the parent, if it is provided (otherwise it becomes a Main Category)

      注意:由于懒惰,我使用了旧的Rails语法,但是对于Rails 3.2,原理是相同的.

      Note: I used the old rails syntax (due to laziness), but for Rails 3.2 the principle is the same.

      在您的categories/index.html.erb中,您可以使用以下内容:

      In your categories/index.html.erb you can use something like this:

      <h1><%= @category.nil? ? 'Main categories' : category.text %></h1>
      <table>
      <% @categories.each do |category| %>
      <tr>
        <td><%= link_to category.text, category_path(category) %></td>
        <td><%= link_to 'Edit', edit_category_path(category) unless category.parent.nil? %></td>
        <td><%= link_to 'Destroy', category_path(category), :confirm => 'Are you sure?', :method => :delete unless category.parent.nil? %></td>
      </tr>
      <% end %>
      </table>
      <p>
        <%= link_to 'Back', @category.parent.nil? ? categories_path : category_path(@category.parent) unless @category.nil? %>
        <%= link_to 'New (sub-category', new_category_path(@category) unless @category.nil? %>
      </p>
      

      它显示所选类别(或主类别)及其所有子类别的名称(在漂亮的表格中).它链接到所有子类别,显示相似的布局,但子类别除外.最后,它添加了新子类别"链接和后退"链接.

      It shows the name of the selected category (or Main Category) and all of its sub-categories (in a nice table). It links to all sub-categories, showing a similar layout, but for the sub-category. In the end it adds a 'new sub-category' link and a 'back' link.

      注意:我的回答有点广泛...我从一个使用类似构造(针对(子)菜单)的项目中复制并修改了它.所以希望我在修改过程中没有破坏任何东西...:)

      Note: My answer became a bit extensive... I copied and modified it from one of my projects that uses a similar construction (for (sub-)menus). So hopefully I did not break anything during the modifications... :)

      这篇关于类别和子类别模型轨道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆