Nested form child only updates if parent changes.

Posted by chap on Stack Overflow See other posts from Stack Overflow or by chap
Published on 2010-03-19T13:10:47Z Indexed on 2010/03/19 13:21 UTC
Read the original article Hit count: 111

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 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.fields_for :assignments do |assignment_form|
      Assignment: assignment_form.text_field :name
    end
  end

  f.submit
end

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby-on-rails3