你好,我試圖爲我的Rails應用程序創建一個重置密碼;但是當我嘗試保存時出現以下錯誤:生成重置密碼令牌不保存在模型上
Validation failed: Password can't be blank, Password is too short (minimum is 6 characters), Password confirmation can't be blank
這是我的用戶模型。
class User < ActiveRecord::Base
attr_accessible :email, :password, :password_confirmation
has_secure_password
before_save { |user| user.email = email.downcase }
before_save :create_remember_token
VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
validates :password, presence: true, length: { minimum: 6 }
validates :password_confirmation, presence: true
def send_password_reset
self.password_reset_token = SecureRandom.urlsafe_base64
self.password_reset_at = Time.zone.now
self.password = self.password
self.password_confirmation = self.password
save!
end
private
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end
方法「send_password_reset」不更新的用戶,我不明白爲什麼試圖保存用戶上,而不是隻更新password_reset_token和password_reset_at。
有人可以幫助我嗎?
非常感謝你。我會接受你的回答,但是stackoverflow說我必須等5分鐘。 – Jean