代码之家  ›  专栏  ›  技术社区  ›  Craig Walker

嵌套对象窗体未按预期工作

  •  3
  • Craig Walker  · 技术社区  · 16 年前

    我想找一个 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.contracents\u 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测试版有问题?

    这是一个由 attr_accessible :name 在菜谱里。这不具体。

    3 回复  |  直到 16 年前
        1
  •  3
  •   theIV    16 年前

    对于你下面的回答,我相信你可以补充一下 ingredients_attributes 作为一个 attr_accessible

        2
  •  1
  •   Craig Walker    16 年前

    我找到了答案:存在 attr_accessible :name

    bug report

        3
  •  0
  •   Craig Walker    16 年前

    我已经证实了 一个3个错误;我建造了 Railscast example 在2.3和3.0中,它都可以正常工作。这意味着这和我的代码有关。

    推荐文章