Ruby on rails 3.2 cocoon:has“许多:通过关系-创造新的-不仅仅是”;添加现有的“;

Ruby on rails 3.2 cocoon:has“许多:通过关系-创造新的-不仅仅是”;添加现有的“;,ruby-on-rails-3.2,simple-form,has-many-through,cocoon-gem,Ruby On Rails 3.2,Simple Form,Has Many Through,Cocoon Gem,我正在开发一个配方应用程序,其中一个配方有标题、说明、说明和含有数量的成分 我有一个配方模型,一个数量模型和一个配料模型: class Recipe < ActiveRecord::Base attr_accessible :description, :instructions, :title, :quantities_attributes, :ingredients_attributes has_many :quantities ha

我正在开发一个配方应用程序,其中一个配方有标题、说明、说明和含有数量的成分

我有一个配方模型,一个数量模型和一个配料模型:

class Recipe < ActiveRecord::Base
  attr_accessible :description, 
    :instructions, 
    :title, 
    :quantities_attributes, 
    :ingredients_attributes

  has_many :quantities
  has_many :ingredients, :through => :quantities

  accepts_nested_attributes_for :quantities,
    :reject_if => :all_blank,
    :allow_destroy => true
  accepts_nested_attributes_for :ingredients
end

class Quantity < ActiveRecord::Base
  attr_accessible :amount, 
    :ingredient_id,
    :ingredient_attributes

  belongs_to :recipe
  belongs_to :ingredient

  accepts_nested_attributes_for :ingredient,
    :reject_if => :all_blank
end

class Ingredient < ActiveRecord::Base
  attr_accessible :name, 
    :ingredient_id

  has_many :quantities
  has_many :recipes, through: :quantities
end
recipes/_quantity_fields.html.haml

= f.simple_fields_for :quantities do |q|
    = render 'quantity_fields', :f => q
        = link_to_add_association 'add ingredient', f, :quantities
= f.submit
    = f.input :amount
    = f.association :ingredient, :collection => Ingredient.all(:order => 'name'), :prompt => 'Choose an existing ingredient'
    = link_to_remove_association "remove ingredient", f
= f.input :name, :hint => 'New Ingredient'
.nested-fields
    = f.input :amount
    = f.association :ingredient, :collection => Ingredient.all(:order => 'name'), :prompt => 'Choose an existing ingredient'
    = link_to_remove_association "remove ingredient", f
    %br
    = link_to_add_association 'or create a new ingredient', f, :ingredient
配方/_配料_字段.html.haml

= f.simple_fields_for :quantities do |q|
    = render 'quantity_fields', :f => q
        = link_to_add_association 'add ingredient', f, :quantities
= f.submit
    = f.input :amount
    = f.association :ingredient, :collection => Ingredient.all(:order => 'name'), :prompt => 'Choose an existing ingredient'
    = link_to_remove_association "remove ingredient", f
= f.input :name, :hint => 'New Ingredient'
.nested-fields
    = f.input :amount
    = f.association :ingredient, :collection => Ingredient.all(:order => 'name'), :prompt => 'Choose an existing ingredient'
    = link_to_remove_association "remove ingredient", f
    %br
    = link_to_add_association 'or create a new ingredient', f, :ingredient
以下是我在食谱中添加新配料时得到的结果:


现在我想知道如何添加一种以前从未添加过的全新成分-你有什么想法吗?

只需添加链接到添加关联标签即可

recipes/_quantity_fields.html.haml

= f.simple_fields_for :quantities do |q|
    = render 'quantity_fields', :f => q
        = link_to_add_association 'add ingredient', f, :quantities
= f.submit
    = f.input :amount
    = f.association :ingredient, :collection => Ingredient.all(:order => 'name'), :prompt => 'Choose an existing ingredient'
    = link_to_remove_association "remove ingredient", f
= f.input :name, :hint => 'New Ingredient'
.nested-fields
    = f.input :amount
    = f.association :ingredient, :collection => Ingredient.all(:order => 'name'), :prompt => 'Choose an existing ingredient'
    = link_to_remove_association "remove ingredient", f
    %br
    = link_to_add_association 'or create a new ingredient', f, :ingredient