Topic: Asynchronously create n grandchildren on add child in nested form

In a Rails 3.2 app, I'm using Cocoon to create a nested from 3 layers deep, i.e., parent, child, grandchild.

In the Controller I'm using

2.times do
      child = @parent.child.build
      2.times { child.grandchild.build }
end

to automatically create blank records within the New view.

I want t replicate this behavior asynchronously within both the new and edit views. i.e., when the user clicks add child, two nested grandchildren should also be created.

I assume the best way to do this is to call a javascript function on new_child.click. But I'm still learning and am unsure what the function should look like to create a nested record.

I'd appreciate any guidance, or pointers to relevant information. What should the javascript look like to create a nested record? And what is the best way to call this function so that it plays nicely with Cocoon's existing functions.

Thanks very much!

Re: Asynchronously create n grandchildren on add child in nested form

I'm just learning how to do this myself, but here's some railscasts that helped me:
http://railscasts.com/episodes?utf8=%E2 … search=197 (It's a two parter, so I just linked to the search results instead). As far as doing it with cocoon, I don't know. Sorry.

Change is a vector. You can have all the change in the world, but if it doesn't go in the right direction, what good is it? | techonamission.com