【发布时间】:2010-05-23 23:02:29
【问题描述】:
我正在尝试使 nested model forms 视图正常工作。据我所知,我做的一切都是正确的,但它仍然无法正常工作。
我正在使用 Rails 3 beta 3。
我的模型符合预期:
class Recipe < ActiveRecord::Base
has_many :ingredients, :dependent => :destroy
accepts_nested_attributes_for :ingredients
attr_accessible :name
end
class Ingredient < ActiveRecord::Base
attr_accessible :name, :sort_order, :amount
belongs_to :recipe
end
我可以按预期使用 Recipe.ingredients_attributes=:
recipe = Recipe.new
recipe.ingredients_attributes = [ {:name=>"flour", :amount=>"1 cup"}, {:name=>"sugar", :amount=>"2 cups"}]
recipe.ingredients.size # -> 2; ingredients contains expected instances
但是,我无法使用参数as shown in the documentation 的散列创建新的对象图:
params = { :name => "test", :ingredients_attributes => [ {:name=>"flour", :amount=>"1 cup"}, {:name=>"sugar", :amount=>"2 cups"}] }
recipe = Recipe.new(params)
recipe.name # -> "test"
recipe.ingredients # -> []; no ingredient instances in the collection
我在这里做错了吗?还是 Rails 3 beta 有问题?
更新
这是由配方中的attr_accessible :name 引起的错误。这不是 Rails3 特有的。
【问题讨论】:
标签: ruby-on-rails ruby forms nested