类别和子类别Rails

时间:2017-05-10 06:07:03

标签: ruby-on-rails categories

所以,我有类别和子类别,我正在尝试添加到项目中,我尝试使用awesome_nested_set gem,但无法找到放置实际类别名称的位置。

我不希望用户能够编辑,更新或删除任何类别或子类别,我只想将它们作为下拉菜单,因此他们可以选择类别然后选择子类别然后进行选择已添加到列表页面。

到目前为止我的代码:

这是category.rb

    class Category < ApplicationRecord
  has_many :subcategories
end

class CreateCategories < ActiveRecord::Migration
  def self.up
    create_table :categories do |t|
      t.string :name
      t.integer :parent_id, :null => true, :index => true
      t.integer :lft, :null => false, :index => true
      t.integer :rgt, :null => false, :index => true

      # optional fields
      t.integer :depth, :null => false, :default => 0
      t.integer :children_count, :null => false, :default => 0
    end
  end

  def self.down
    drop_table :categories
  end
end

class Category < ActiveRecord::Base
  acts_as_nested_set
end

这是_form.html.erb部分类别:

<%= f.select :parent_id, nested_set_options(Category, @category) {|i| "#{'-' * i.level} #{i.name}" } %>

所以,我的问题是,我在哪里放置类别名称以及如何将它们链接到潜艇?

1 个答案:

答案 0 :(得分:0)

没关系。关闭这些东西的人现在可以继续这样做。我发现了一个解释它的YouTube视频。

这与问题没有太大关联,但视频是12周作业板视频中的MacKenzie Child 12应用程序....我没有链接但它应该出现在youtube搜索中,如果你输入。

通过观察他为他创造的各类工作创建类别,我想出了我的答案。

再次感谢。

相关问题