Solution w/o accepts_nested_attributes_for
This is my 2nd ever answer on SO, but I came across this problem multiple times, couldn't as of this writing find a reasonable solution and finally figured it out myself after doing a little nested_attributes hypothesizing and wanted to share my best understanding of it. I believe the strong params line you're after is this:
product_params = params.require(:products).permit(product_fields: [:value1, :value2, etc...])
I don't know exactly what your form looks like, but you will need to have a fields_for to nest the params in the product_fields(or any_name):
form_for :products do |f|
f.fields_for product_fields[] do |pf|
pf.select :value1
pf.select :value2
...
end
end
This will allow the permit() to accept a single explicit hash
product_fields => {0 => {value1: 'value', value2: 'value'}}
instead of the key/value pairs
0 => {value1: 'value', value2: 'value'}, 1 => {value1: 'value', value2: 'value'}, etc...
which you'd otherwise have to name individually:
.permit(0 => [value1: 'value', value2: 'value'], 1 => [...], 2 => [...], ad infinitum)
This allows you to update multiple products without having to use a parent model accepting nested attributes. I just tested this on my own project in Rails 4.2 and it worked like a charm. As for creating multiple: How would I save multiple records at once in Rails?.
As for a counter, you may need to iterate over each model individually:
product_params[:product_fields].keys.each_index do |index|
Product.create!(product_params.merge(counter: index))
end
Thought it's been so long you probably resolved that on your own. :-)