在此视频(10 秒)中,您可以看到嵌套属性仅在其父模型更改时才会更新。
使用 rails 3.0.0.beta 并且完整的项目在 github 上。
型号及形式总结:
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