ruby-on-rails - 加密的出生日期未保存在 sinatra/rails 的数据库中
问题描述
这是我的代码:
病人档案.rb
class PatientProfile < ApplicationRecord
belongs_to :patient
............
attr_encrypted :dob, key: Base64.decode64(SECRET_KEY)
validates_presence_of :dob
end
db/migrate/20200618205840_patient_profiles.rb
class PatientProfiles < ActiveRecord::Migration[6.0]
def change
create_table :user_profiles do |t|
t.string :encrypted_gender
t.string :encrypted_gender_iv
t.string :encrypted_address
t.string :encrypted_address_iv
.....................
t.date :encrypted_dob
t.date :encrypted_dob_iv
t.integer :patient_id, index: true
t.timestamps
end
end
end
患者控制器.rb
class PatientsController < ApplicationController
# POST /api/v1/signup
post '/signup' do
patient = Patient.new(params[:patient])
patient.password = params[:patient][:password]
patient.unique_code = SecureRandom.hex(6)
patient.confirmation_token = (SecureRandom.random_number(9e4) + 1e4).to_i
patient.confirmation_sent_at = Time.now
if patient.save
token = JsonWebToken.encode(patient_id: patient.id)
send_email(patient,
"Account Verification",
"Your verification code is #{patient.confirmation_token}")
response = { patient: patient_response(patient), token: token }
[200, response.to_json]
else
halt 422, error_message(patient.errors.full_messages)
end
rescue ActiveRecord::RecordInvalid => error
halt 422, error_message(error.message)
end
在邮递员中,我们可以发布日期并显示已发布。但在数据库中 dob 字段为空。
这有什么问题?
提前致谢。
这是日志:
解决方案
我变了:
t.date :encrypted_dob
t.date :encrypted_dob_iv
至
t.string :encrypted_dob
t.string :encrypted_dob_iv
它工作正常并将加密的 dob 保存在数据库中。
推荐阅读
- amazon-web-services - AWS::ApiGatewayV2::Integration 如何在 CloudFormation 中使用 MessageAttributes
- python - 检查语音通道是否为空?不和谐.py
- html - 为什么在减小窗口宽度后页脚不粘在底部?
- ruby - 如何将带有 PostCSS 清除的 Tailwind CSS 添加到 Rails 5?
- python - 线程未正确打印
- apache-arrow - 您如何判断给定库版本的 Apache 箭头格式版本?
- java - 如何更改 JFrame 顶部按钮和颜色?
- mysql - 什么是检查数组是否在 MySQL 记录中的聪明方法?
- amazon-web-services - Run Command 不会在 EC2 实例 (AWS) 上保存文件
- javascript - 将旋转代理端点附加到 URL