Rails 7加密电子邮件+设计(错误)



我正在使用Rails 7 +设计gem。我正在尝试加密电子邮件字段(由设计生成)。我成功地执行了rails db:encryption:init并设置了我的凭据。

用户模式:

class User < ApplicationRecord

encrypts :email, deterministic: true, downcase: true
validates :email, presence: true

devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable, :confirmable
end

当我尝试访问登录和注册路径时,我得到这些错误:

ActiveRecord::Encryption::Errors::Decryption in Devise::Sessions#new
ActiveRecord::Encryption::Errors::Decryption in Devise::Registrations#new

都指向视图中的这一行:

<%= f.input :email,

甚至当我尝试通过rails控制台创建一个新用户时,我得到:

unexpected token at '' (JSON::ParserError) 
ActiveRecord::Encryption::Errors::Encoding (ActiveRecord::Encryption::Errors::Encoding)
ActiveRecord::Encryption::Errors::Decryption (ActiveRecord::Encryption::Errors::Decryption)

我该如何解决这个问题?新的Rails 7 ActiveRecord加密与设计可用吗?

非常感谢。

看起来这应该有助于解决您的错误https://github.com/heartcombo/devise/issues/5436#issuecomment-1014152052

有两种方法,你可以设置:

config.active_record.encryption.support_unencrypted_data = true

或更改email字段的迁移:

null: false, default: ""

您需要从用户中删除default: ""

class RemoveDefaultEmailFromUsers < ActiveRecord::Migration[7.0]
def up
change_column_default(:users, :email, nil)
end
def down
change_column_default(:users, :email, '')
end
end

最新更新