MassAssignmentSecurity使用attr_encrypted(attr_encryptor)gem时出错

时间:2012-06-08 23:14:45

标签: ruby-on-rails-3 mass-assignment attr-encrypted

对于我的rails 3.2.3 app,我使用的是attr_encryptor,它是atpal_encrypted的danpal的一个分支。我已按照here给出的说明进行操作,但在尝试创建新的Patient记录时收到以下错误消息:

ActiveModel::MassAssignmentSecurity::Error in PatientsController#create

Can't mass-assign protected attributes: mrn, last_name, first_name, date_of_birth(1i), date_of_birth(2i), date_of_birth(3i)

正如说明所述,我已将encrypted_#{field}encrypted_#{field}_saltencrypted_#{field}_iv列添加到我的Patients表中,同时删除了未加密的对应项。

Patient模型如下:

class Patient < ActiveRecord::Base
  attr_accessible :age, :gender
  attr_encrypted :last_name, :key => 'key 1'
  attr_encrypted :first_name, :key => 'key 2'
  attr_encrypted :mrn, :key => 'key 3'
  attr_encrypted :date_of_birth, :key => 'key 4'
  # ...
end

我的create控制器中的Patient方法如下:

PatientsController < ApplicationController
  # ...
  def create
    @patient = Patient.new
    @patient.first_name = params[:patient][:first_name]
    @patient.last_name = params[:patient][:last_name]
    @patient.mrn = params[:patient][:mrn]
    @patient.date_of_birth = Date.new(params[:patient]['date_of_birth(1i)'],
                                      params[:patient]['date_of_birth(2i)'],
                                      params[:patient]['date_of_birth(3i)'])
    if @patient.save
      # do stuff
    else
      # do other stuff
    end
  end
  # ...
end

我做错了什么?在此先感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您需要使用attr_accessibleattr_encrypted标记这些属性,因为后者并不意味着前者。

这也可能与日期字段相关:Correct way to handle multiparameter attributes corresponding to virtual attributes