In my Rails app, I would like to pass a Javscript array into the new 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 new view, etc.?
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.
Currently, my action controller looks like this:
def new
x = Integer(params[:total_inserts_param])
@report_group = Array.new
x.times do
@report_group.push(Report.new)
end
end
where total_inserts_param 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 new 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!
I would much rather get the array into the controller, generate a Report.new for each JSON in the array, then populate the input fields created by the controller with the attributes of the JSONs.
I'm fairly new to Rails, so any insight into how to approach this would be much appreciated.