演示模式並不一定是這裏最合適,其實。
你可能會得到futher只要使用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
然後在你的形式做這樣的事情(在HAML):
= 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次)。