How to visualize one association automatically using a cocoon

I have a cocoon that works with a nested form, if you click the Add Field link, it inserts input fields. How to make the first input automatically, and then insert additional inputs when you click "add field"?

+4
source share
2 answers

Use your code in your controller. In the job code below, this is the model and profile of accepts_nested_attributes_for jobs. Replace @profile with any form. The second line is what will build the form fields if the form fields no longer exist.

def new
    @profile = current_user.profile
    1.times {@profile.jobs.build} unless current_user.profile.jobs.any?
end

, . , :

def new
    @profile = current_user.profile
    @profile.jobs.build unless current_user.profile.jobs.any?
end
+6

, jQuery ( Cocoon anyways), " " Cocoon :

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

"" , "" , , . script " ".

+1

All Articles