title: Rails - passing a JSON from a view into a controller
body: <p>In my Rails app, I would like to pass a Javscript array into the <code>new</code> action controller - the array is created from user input in one of the other views, and is composed of JSONs.  What is the best method to a) pass the array of JSONs to the controller, and b) parse the array in the controller so that I can do things like get the array length, render the JSONs into the <code>new</code> view, etc.?</p>
  
  <p>As I have built it, the app works fine, but I can see that the way that I have done things will cause problems as I continue to develop it.</p>
  
  <p>Currently, my action controller looks like this:</p>
  
  <pre><code>def new
  
      x = Integer(params[:total_inserts_param])
  
      @report_group = Array.new
      x.times do
        @report_group.push(Report.new)
      end
  end
  </code></pre>
  
  <p>where <code>total_inserts_param</code> is a count of the array length calculated in the preceding view and passed in via URL, while the actual content of the JSONs are passed between the views via stringified localStorage objects; the content of those objects is parsed out and inserted into the variable number of form input fields in the <code>new</code> view generated by the controller.  This is certainly one way to pass data around a Rails app, but I'm pretty sure it's not a very good way!</p>
  
  <p>I would much rather get the array into the controller, generate a <code>Report.new</code> for each JSON in the array, then populate the input fields created by the controller with the attributes of the JSONs.</p>
  
  <p>I'm fairly new to Rails, so any insight into how to approach this would be much appreciated.</p>