【发布时间】:2023-03-06 12:20:01
【问题描述】:
我曾尝试对此进行研究,但奇怪的是似乎没有其他人遇到此问题,或者我的搜索不正确。无论如何,这里是我的 user.rb。我可以毫无问题地创建用户,并且我的自定义字段“pin”已设置。我可以更新“pin”字段。但是,如果尝试更改名称、电子邮件或密码,它们会失败,因为根据我的模型中的验证,pin 是空白的。如果我不更新,如何跳过密码验证?此外,如果我更新我的电子邮件,它仍然会发送确认信息,但是当我单击链接时,它会再次验证 pin 并且确认失败。另外,我也不想在 after_validation 帮助器中重新生成哈希。
class User < ApplicationRecord
has_many :profiles, dependent: :destroy
has_many :general_settings, dependent: :destroy
has_many :profile_settings, through: :profiles
has_many :tags, dependent: :destroy
has_many :videos, through: :profiles
belongs_to :account_type
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable
after_validation :create_parent_digest
validates :pin, presence: true, length:{ is: 4 }, confirmation: true
attr_accessor :pin, :pin_confirmation
def valid_pin?(pin)
Devise::Encryptor.compare(self.class, parent_digest, pin)
end
private
def create_parent_digest
self.parent_digest = Devise::Encryptor.digest(self.class, pin)
end
end
【问题讨论】:
标签: ruby-on-rails activerecord devise