对于我的 rails 3.2.3 应用程序,我使用的是 attr_encryptor,它是 attr_encrypted 的 danpal 的一个分支。我已按照此处给出的说明进行操作,但在尝试创建新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}_salt
和encrypted_#{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
我究竟做错了什么?在此先感谢您的帮助!