0

レシピにタイトル、説明、指示、材料と量が含まれるレシピアプリケーションに取り組んでいます

私はレシピモデル、数量モデル、材料モデルを持っています:

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

単純なフォームで繭を使用して、ネストされたモデルフォームを作成しています。これまでのところ、次のとおりです。

レシピ/_form.html.haml

= f.simple_fields_for :quantities do |q|
    = render 'quantity_fields', :f => q
        = link_to_add_association 'add ingredient', f, :quantities
= f.submit

レシピ/_quantity_fields.html.haml

    = f.input :amount
    = f.association :ingredient, :collection => Ingredient.all(:order => 'name'), :prompt => 'Choose an existing ingredient'
    = link_to_remove_association "remove ingredient", f

レシピ/_ingredient_fields.html.haml

= f.input :name, :hint => 'New Ingredient'

レシピに新しい材料を追加すると、次のようになります。

今、これまでに追加されたことのないまったく新しい成分をどのように追加できるかを考えています。何かアイデアはありますか?

4

1 に答える 1