简体   繁体   中英

ActiveModel::ForbiddenAttributesError in Rails 4?

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

将以这种方式编码的属性数组列入白名单...

params.permit(measurements: [ :name, :location_1, :location_2 ])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM