开发者

rails3 html5 jquery dynamically added nested forms, presenter pattern?

开发者 https://www.devze.com 2023-01-17 16:57 出处:网络
Job has many task, task has many notes How should such a form look like ? With partials so I can enter the whole job from /jobs/new , and add new tasks from /jobs/2/tasks/new with possibility to add

Job has many task, task has many notes

How should such a form look like ? With partials so I can enter the whole job from /jobs/new , and add new tasks from /jobs/2/tasks/new with possibility to add notes from there, and of course the possibility to add new notes from /jobs/2/tasks/5/notes/new ?

Is this a good place to use the presenter pattern ? When yes, which library should I use ? I have tried the rails3 b开发者_运维百科ranch of active_presenter on github but I had some troubles with the form partials.

Have somebody a good example for such a task ?

There are plenty of presenter tutorials, nested forms tutorials, and unobtrusive javascript tutorials, but not only one have explained all together.

It would be nice if someone pasted a tutorial using html5 and rails3 examples


Presenter pattern isn't necessarily the best fit here, actually.

You'll probably get futher by just using 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

And then in your form doing something like (in 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"

You may have to actually initialise some tasks/notes for new jobs or the associated record forms might not show up. E.g., do something like 3.times { @job.tasks.build } to create 3 blank tasks (and therefore display the task sub-form 3 times).

0

精彩评论

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