我是新手,我正在学习RoR . 我有Rails 5.2.1,我正在尝试实现祖先的宝石 . 我已经看过Railscasts和其他视频,但我仍然无法继续前进 .
所以我想要的:
我希望用户为汽车零件创建报价,但在新表格中,我希望能够选择类别/子类别,然后输入我拥有的其他详细信息,然后提交表单以创建报价 . 例如
汽车 - >制动器 - >制动蹄片
因此,在选择Brake Pads后,他可以创建报价 .
是)我有的:
#category.rb
class Category < ApplicationRecord
has_ancestry
has_many :parts
end
#part.rb
class Part < ApplicationRecord
belongs_to :category
end
我没有CategoriesController,如果没有必要,我现在不想创建一个控制器,因为我将使用我想要的类别为我的数据库播种,就是这样 . 现在我已经在控制台中创建了一个以使其工作:例如
car = Category.create!(name: "Car")
brakes = Category.create!(name: "Brakes", parent: car)
brake_pads = Category.create!(name: "Brake Pads", parent: brakes)
我已经运行了迁移 rails g migration add_category_to_parts category:references
.
我的观点是:
#views/parts/new.html.haml
.container
%h2.center Create offer
= simple_form_for(@part, html: {class: "form-group" }) do |f|
.form-group.col-md-8.col-md-offset-2..well
= f.input :title
= f.input :make_name, label: "Make"
= f.input :code, label: 'Part number'
= f.association :condition, label_method: :name, prompt: "-"
= f.input :description
.form-actions
= f.button :submit, class: "btn btn-primary btn-dark-blue"
问题是:
这是使用祖先的正确方法吗?如果是,我如何在我的new.html.haml表单中使用3个下拉列表呈现类别/子类别(每个子类别一个,因为我将有许多类别/子类别,或者如果不可能,请帮我一个嵌套)所以用户可以选择它们然后创建优惠吗?