Reputation: 1576
I am getting the following error when trying to update a Rails model via the ActiveModel update method:
ActiveModel::ForbiddenAttributesError
I am aware of the strong parameters requirement in Rails 4 per the link below, but how do I whitelist the params in my case - an array of hashes? I cannot make sense of the documentation.
http://guides.rubyonrails.org/action_controller_overview.html#strong-parameters
Here are the json params that I am trying to process:
{
id: 1,
month: 'April',
measurements: [
{ id: 1, name: 'PT', location_1: '1.1', location_2: '1.2' },
{ id: 1, name: 'OT', location_1: '1.1', location_2: '1.2' },
.
.
]
}
Controller action:
def update
#Trying to update all measurements associated with this parent object
#params.permit(measurements: [{ :name, :location_1, :location_2 } ])
#This attempt causes a syntax error
measurements = params[:measurements]
measurements.each do |measurement|
current_measurement = Measurement.find(measurement[:id])
new_measurement = measurement.except(:id)
current_measurement.update(new_measurement)
end
.
.
end
Upvotes: 1
Views: 1242
Reputation: 36880
to whitelist an array of attributes you'd code it this way...
params.permit(measurements: [ :name, :location_1, :location_2 ])
Upvotes: 2