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

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

问题描述

在不使用任何 gem 的情况下,如何在 Rails 中执行此操作?

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

我有一张由 | 组成的表身份证 |一级 |2级|

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

Level 1 为主类别,Level 2 为子类别

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

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

I would like it displayed in the view like above.

在互联网上环顾四周后,似乎每个人都推荐使用行为类似树的 gem,但我想避免使用它们,因为我对 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", :optional => true
end

  • has_many 定义了模型类型Categorysubcategories 关联.即它使用相同的表.
  • belongs_to 定义了与父类别的关系,可通过 @category.parent_category
  • 访问

    • 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, accessible via @category.parent_category
    • 有关模型关联的更多信息,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
            # table name should be in plural 
            t.references  :parent_category, foreign_key: { to_table: :categories }
            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.parent_category
        # 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 = Category.find(params[:id]) unless params[:id].nil?
      end 
      

      它创建一个新类别并设置父类别,如果提供(否则它成为主类别)

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

      注意:我使用了旧的 rails 语法(由于懒惰),但对于现代版本的 Rails,原理是相同的.

      Note: I used the old rails syntax (due to laziness), but for modern versions of Rails 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_category.nil? ? categories_path : category_path(@category.parent_category) 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.

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

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