3

Suppose I have these models which I plan to add/delete on the same 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 a "add a question" link and then a "add an answer" link, and that when clicked, new items are added accordingly.

What I would like to know is if it's possible to have Cocoon add a "child" nested item automatically when the link is clicked. For example (using above models example), when the user clicks on the "add a question" link, I would like to create a "new answer" record automatically after the "new question" record is created.

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

Is this possible with Cocoon?

Community
  • 1
  • 1

1 Answers1

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

this worked for me.

DhatchXIX
  • 437
  • 1
  • 3
  • 17