我有一个带有 reset_token 方法的成员模型(它为用户分配一个新令牌,以便向他们发送电子邮件以重置他们的密码)。但是 update_attribute 永远不会在数据库中保存任何内容。我已将 :new_password_token 分配给 attr_accessible 和 attr_accessor。日志获取盐和令牌,但它总是返回 nil
def self.reset_token(email)
member = find_by_email(email)
if member
#Reset token, and then send email
salt = BCrypt::Engine.generate_salt
logger.error "Salt is #{salt}"
token = BCrypt::Engine.hash_secret(email, salt)
logger.error "token is #{token}"
if member.update_attribute(:new_password_token, token)
member
end
end
nil
end
调用它的控制器方法:
def reset_password
@member = Member.reset_token(params[:email])
if @member
redirect_to(root_url, :notice => "Please check your email for instructions")
else
redirect_to(root_url, :notice => "Sorry we have no record of your account")
end
end