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

查看:68
本文介绍了Rails:类别和子类别模型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

我有一个包含|的表。 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

我希望它显示在

在互联网上环顾四周后,每个人似乎都建议使用像树上的宝石那样的宝石,但我想避免使用它们,因为m对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 定义模型类型类别子类别关联。即它使用相同的表。

  • 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_many belongs_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... :)

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

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