【问题标题】:Categories and Subcategories Rails类别和子类别 Rails
【发布时间】:2017-10-08 15:27:04
【问题描述】:

所以,我有一些类别和子类别要添加到项目中,我尝试使用 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}" } %>

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

【问题讨论】:

  • 你有什么问题?
  • 编辑文章结尾

标签: ruby-on-rails categories


【解决方案1】:

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

这与问题并不太相关,但该视频是 MacKenzie Child 12 个应用程序在 12 周工作委员会视频中......我没有指向它的链接,但如果你输入它应该会出现在 youtube 搜索中.

我通过观察他为他创建的各种类型的工作创建类别找到了答案。

再次感谢。

【讨论】:

    猜你喜欢
    • 2019-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-05
    • 2012-10-14
    相关资源
    最近更新 更多