Rails - Authlogic persistence_token问题

时间:2011-04-18 02:29:23

标签: ruby-on-rails authlogic

我正在尝试使用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]被设置,persistence_token值在@user对象中发生变化。

1 个答案:

答案 0 :(得分:2)

htanata说的是什么。您没有设置密码确认。你能告诉我们你得到的错误信息吗?

首先,您需要设置password_confirmation,否则您将收到验证错误。