2
我使用attr_encrypted来支持我的rails应用程序中的客户数据的加密。当密钥是静态的时候,gem正在工作,但是当我尝试使用proc从params[:key]
(因此我不需要存储密钥)在控制器中获取密钥时,密钥使用不正确。我当前的代码是:使用attr_encrypted和客户提供的密钥(未存储在数据库中)
客户模式:
class Customer < ActiveRecord::Base
belongs_to :store
attr_encrypted :phone, key: proc { |key| "#{key}" } , :attribute => 'phone_number'
客户控制器:
class CustomersController < ApplicationController
def create
if params[:customer][:phone_number].present?
@customer = Customer.new(customer_params)
@customer.store_id = @store.id
key = params[:key]
@customer.phone = params[:customer][:phone_number]
@customer.phone_number
if @customer.save
return_response['new_customer']=1
else
render json: @customer.errors, status: :unprocessable_entity
end
宝石说,我也可以通过一类方法的关键。有人对我如何使用它来实现不将密钥存储在数据库中的目的有任何想法吗?任何帮助将不胜感激。