且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Rails:类别和子类别模型轨道

更新时间:2023-11-29 22:41:58

创建一个引用 本身 用于子类别(或子子类别等):

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.