类别和子类模型轨道(categories and sub-categories model rail

2019-07-17 22:18发布

不使用任何宝石我怎么在铁轨做到这一点?

大类
子类别
子类别
子类别

大类
子类别
子类别
子类别

大类
子类别
子类别
子类别

我有一个包含的表| ID | 1级| (第二级)|

电平1是主要的类别和级别2是子类别

我想它显示在像上面的观点。

互联网上的每个人都四处寻找后,似乎使用的行为样一个树宝石推荐,但我想避免使用它们,因为我是相当新的轨道,我想了解如何做的事情,而不是求助于宝石。

你的帮助是非常apreciated

模型:

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>

Answer 1:

创建有一个子类(或子子类等),以自身引用的模型:

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定义了一个subcategories的模型类型的关联Category 。 也就是说,它使用相同的表。
  • 所述belongs_to限定回父类别的关系(可选,不是必需的)

有关模型关联,更多信息has_manybelongs_to ,阅读协会基础知识手册 。

要创建该表使用这种迁移:

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

:本表格式(略)不同的比你建议,但我想,这是不是一个真正的问题。

该迁移指南包含了数据库迁移的详细信息。

在您的控制器使用

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

找到所有类别没有父母,即主类别

为了找到任何类别使用的所有子类:

# 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

要添加一个新的类别的使用:

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

它创建了一个新的类别,设置家长,如果提供(否则就成了一个主类别)

注:我使用的旧钢轨语法(由于懒惰),但为Rails 3.2的原理是一样的。

在你的categories/index.html.erb你可以使用这样的事情:

<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>

它显示选定的类别(或主类别)的名称及其所有子类的(在一个不错的表)。 它链接到所有的子类别,呈现出类似的布局,但对于子类。 最后,它增加了一个“新的子类别”链接和“返回”的链接。

:我的答案变得有点丰富......我复制并使用了类似的结构(对于(子)菜单)我的一个项目修改了它。 所以希望我没有修改过程中破坏任何东西... :)



文章来源: categories and sub-categories model rails