I built a form_for with 4 associated models. I created nested fields_for which can be added dynamically with the code below. My form works if I call the 'edit' method but if I use the method below to add new data the fields_for remains empty and are not shown.
def link_to_add_fields(name, f, association)
new_object = f.object.send(association).klass.new
id = new_object.object_id
fields = f.fields_for(association, new_object, child_index: id) do |builder|
render(association.to_s.singularize + "_fields", f: builder)
end
link_to(name, '#', class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")})
end
This works fine if I only wants to add one form of a model. But if the added content which have fields_for for nested fields_for the text_fields are not created. In my opinion it is because I do not build the associations. How can I fix this method to do this?