实际上,演示者模式不一定是最适合这里的。
你可能会因为使用
accepts_nested_attributes_for
:
# Models
class Job < ActiveRecord::Base
has_many :tasks, :autosave => true
accepts_nested_attributes_for :tasks
end
class Task < ActiveRecord::Base
belongs_to :job
has_many :notes, :autosave => true
accepts_nested_attributes_for :notes
end
class Note < ActiveRecord::Base
belongs_to :task
end
然后以你的形式做一些像(哈姆语)的事情:
= form_for @job do |job|
= job.text_field :name # or whatever your Job attributes are
= job.fields_for :tasks do |task|
= task.text_field :name
= task.check_box_field :complete
= task.fields_for :notes do |note|
= note.text_field :body
= job.submit "Create Job"
您可能需要为新作业实际初始化一些任务/注释,或者相关的记录表单可能不会出现。例如,做一些像
3.times { @job.tasks.build }
创建3个空白任务(因此显示任务子窗体3次)。