开发者

Nested form child only updates if parent changes

In this video (10 sec) you can see that the nested attribute is only updated if it's parent model is changed.

Using rails 3.0.0.beta and the full project is on github.

Summary of models and form:

class Project < ActiveRecord::Base
  has_many :tasks
  accepts_nested_attributes_for :tasks
end

class Task < ActiveRecord::Base
  belongs_to :project
  has_many :assignments
  accepts_nested_attributes_for :assignments
end

class Assignment < ActiveRecord::Base
  belongs_to :task
end


form_for(@project) do |f|

  Project: f.text_field :name

  f.fields_for :tasks do |task_form|
    Task: task_form.text_field :name

    task_form开发者_StackOverflow中文版.fields_for :assignments do |assignment_form|
      Assignment: assignment_form.text_field :name
    end
  end

  f.submit
end


EDIT2: The same code works perfectly in Rails 2.3.5. It seems to be a bug in beta version.

EDIT: I wrote a test in my app to simulate this. Here's the result.

  test "should update empresa" do
    addr = empresas(:ufba).address
    put_with users(:s_one), :update, :id => empresas(:ufba).to_param,
             :empresa => { :address_attributes => {:id => empresas(:ufba).address.to_param,
                                                   :city => "Feira de Santana"}}
    assert_not_nil assigns(:empresa)
    assert_not_same addr, assigns(:empresa).address, "Endereco nao foi atualizado"
    assert_redirected_to empresa_path(assigns(:empresa))
  end

It runs without errors.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜