Is there a way to dynamically add X the number of nested form fields? For example, if we have a selection menu:
Select Menu -1 -2 -3 -4
And the user selects 3, then creates 3 nested form fields.
I looked at the Railscast form on a nested model, but for me it already has one set of fields for already created ones and just inserts them every time a link is clicked. I would like to dynamically insert the quantity X every time the selection menu changes.
Here is the code from Railscast:
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
source
share