2

Rails 3.2.3 アプリでは、attr_encrypted の danpal によるフォークである attr_encryptor を使用しています。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}_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

私は何を間違っていますか?助けてくれてありがとう!

4

1 に答える 1

0

後者は前者を意味しないため、これらの属性をattr_accessible同様にマークする必要があります。attr_encrypted

これは、日付フィールドにも関連する可能性があります:仮想属性に対応するマルチパラメータ属性を処理する正しい方法

于 2013-07-17T05:13:29.207 に答える