【发布时间】:2011-04-18 02:29:23
【问题描述】:
我正在尝试使用http://www.binarylogic.com/2008/11/16/tutorial-reset-passwords-with-authlogic/ 的教程实现重置密码功能
并面临困难。问题似乎来自persistence_token 与数据库中的内容不匹配。
persistence_token 何时设置/更新?
我的密码重置代码与教程一模一样,这里是:
class PasswordResetsController < ApplicationController
before_filter :require_no_user
before_filter :load_user_using_perishable_token, :only => [ :edit, :update ]
def create
@user = User.find_by_email_address(params[:email])
if @user
@user.deliver_password_reset_instructions!
flash[:notice] = "Instructions to reset your password have been emailed to you"
redirect_to login_path
else
flash.now[:error] = "No user was found with email address #{params[:email]}"
render :action => :new
end
end
def update
@user.password = params[:password]
if @user.save
flash[:success] = "Your password was successfully updated"
redirect_to @user
else
render :action => :edit
end
end
private
def load_user_using_perishable_token
@user = User.find_using_perishable_token(params[:id])
unless @user
flash[:error] = "We're sorry, but we could not locate your account"
redirect_to new_password_reset_path
end
end
方法load_user_using_perishable_token 正在从数据库中正确加载值,但是,对@user.password = params[:password] 的调用正在将peristence_token 修改为不同的值。随后的调用@user.save 失败,因为它找不到修改后的persistence_token 的记录。
任何解决这个问题的想法都会很有帮助。
编辑澄清:
1) 没有错误,@user.save 返回 false。 @user.save 触发以下 SQL 查询:
SELECT `users`.id FROM `users` WHERE (`users`.`persistence_token` =
BINARY '53a6fd82a1da50c299230afd2b7911e7774f6fd62a714909194d5d84e2094444d808a71992bb8b11bc572fd865e9956681dea61bf46e3f65d9d41c53a6b0ec90' AND `users`.id <> 7) LIMIT 1
查询中使用的 persistence_token 值与数据库值不匹配。
2) 我不确定这是否是一个线索,但在调试器中我注意到无论何时 @user.password = params[:password] 被设置,@user 对象中的 persistence_token 值改变。
【问题讨论】:
-
失败时的错误信息是什么?我怀疑它失败是因为您没有设置
@user.password_confirmation,因此验证失败。 -
你是对的,原来它需要密码确认。即使我的用户模型没有密码确认字段,也需要设置该值才能成功更新现有密码。感谢您为我指明正确的方向。