首页 > 解决方案 > 加密的出生日期未保存在 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 字段为空。

这有什么问题?

提前致谢。

这是日志:

这是我的日志

标签: ruby-on-railsdatabasesinatradate

解决方案


我变了:

  t.date :encrypted_dob
  t.date :encrypted_dob_iv

  t.string :encrypted_dob
  t.string :encrypted_dob_iv

它工作正常并将加密的 dob 保存在数据库中。


推荐阅读