title: Rails - dynamically add multiple nested form fields
body: <p>Is there a way to dynamically add X number of nested form fields? For example if we have a select menu:</p>
  
  <p>Select Menu
  -1
  -2
  -3
  -4</p>
  
  <p>And the user selects 3, then create 3 nested form fields.</p>
  
  <p>I have watched the Railscast on Nested model form but to me this already has the one set of fields_for already created and simply inserts them each time the link is clicked. I would like to dynamically insert X amount each time the select menu changes.</p>
  
  <p>Here is some code from the Railscast:</p>
  
  <pre><code>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
  </code></pre>