Rails 4中的ActiveModel :: ForbiddenAttributesError?

时间:2015-03-17 22:35:25

标签: ruby-on-rails ruby-on-rails-4

尝试通过ActiveModel更新方法更新Rails模型时出现以下错误:

ActiveModel::ForbiddenAttributesError

我知道Rails 4中每个链接的强参数要求,但是如何将我的情况下的参数列入白名单 - 一系列哈希?我无法理解文档。

http://guides.rubyonrails.org/action_controller_overview.html#strong-parameters

以下是我正在尝试处理的json params:

{
    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' },
        .
        .
    ]
}

控制器操作:

  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

1 个答案:

答案 0 :(得分:2)

将您以这种方式编码的一系列属性列入白名单......

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