title: Form with dynamic number of items
body: <p>Just thinking about the best way to build an Order form that would (from user-perspective):</p>
  
  <ul>
  <li>Allow adding multiple items.</li>
  <li>Each item has Name, Job Type drop-down and File upload.</li>
  <li>User can add or remove items.</li>
  <li>User must provide at least one item.</li>
  <li>All the items should be validated (eg: Name, JobType and File are required).</li>
  <li>When he hits the Submit, an Order should be created with all the items in it.</li>
  </ul>
  
  <hr>
  
  <p>So the model looks like: <code>User -1---*-Order-1---*- OrderItem-*--1-JobType</code>. Additionally OrderItem includes number of attributes, let's say <code>name</code>,  <code>file</code>.  </p>
  
  <hr>
  
  <p>Now I would like to have as skinny controller as possible. Better with the 'standard' code like:</p>
  
  <pre><code>class OrdersController &lt; ApplicationController
    def create
      @order = Order.new(params[:order])
      if @order.save
        redirect_to account_url
      else
        flash.now[:error] = "Could not save Order"
        render :action =&gt; "new"
      end
    end
  end
  </code></pre>
  
  <p>The most interesting thing is <strong>how a view should look like so that rails automatically binds all the values posted to the correct objects</strong>?</p>
  
  <p>For now I don't worry about degradation from JavaScript, but rather the template for the items to be added and linking all that with the validation (probably as a partial).</p>
  
  <p>I expect a template to look like the one below, but not sure.</p>
  
  <pre><code>&lt;input type='text' name='order[order_items[name[]]][]' /&gt;
  &lt;select name='order[order_items[job_type[]]][]'&gt;...&lt;/select&gt;
  &lt;input type='file' name='order[order_items[file[]]][]' /&gt;
  </code></pre>
  
  <p>I have to clone this template in order to add item using JS.</p>
  
  <hr>
  
  <p>So the answer should:</p>
  
  <ul>
  <li>Include the view or part of it that demonstrates:
  
  <ul>
  <li>correct binding of the post values;</li>
  <li>correct validation for all items;</li>
  </ul></li>
  <li>Include related part of model.</li>
  <li>Include changes to the controller IF and ONLY IF necessary.</li>
  <li>(JavaScript is not required)</li>
  </ul>
  
  <p>Ideally I would like to see a sample if somebody knows one (maybe somewhere on GitHub).  </p>
  
  <p>And the last note, <strong>please</strong>, check your solutions before posting.</p>
  
  <p>Thanks,<br>
  Dmitriy.</p>