Auto add baby to nested form using cocoon

Suppose I have these models that I plan to add / remove in one form:

class Survey < ActiveRecord::Base
  has_many :questions
end

class Question < ActiveRecord::Base
  belongs_to :survey
  has_many :answers
end

class Answer < ActiveRecord::Base
  belongs_to :question
end

      

I already have Cocoon working with an "add question" link and then a "add answer" link and that new items are added on click.

What I would like to know is the possibility if Cocoon will automatically add a "child" sub-element when the link is clicked. For example (using the example above models), when a user clicks on the "add question" link, I would like to create a "new answer" record automatically after creating a "new question" record.

I found this link ( Rails - Dynamically builds deeply nested objects (Cocoon / nested_form) ), but I couldn't fully understand it.

Is this possible with Cocoon?

+3


source to share


1 answer


$(document).ready(function() {
  $("a.add_fields").click();
});

      



it worked for me.

0


source







All Articles