是否可以检查:password
字段中输入的内容是否与模型中的:current_password
字段不同?
像
这样的东西validates :current_password, :format => { :with => :password, :message => "Current password can't be the same as the password" }
哪种不起作用的正确方法是什么?
答案 0 :(得分:0)
首先,我希望您不要将密码存储为纯文本!
其次,自定义验证适用于您:
validate :password_is_not_the_same
def password_is_not_the_same
errors.add(:password, 'Current password can\'t be the same as the password') if BCrypt::Password.new(password_digest) == password
end
修改强>
validate :password_is_not_the_same
def password_is_not_the_same
errors.add(:password, 'Current password can\'t be the same as the password') if current_password == password
end