2013-03-13 110 views
2

我試圖確保用戶的重置新密碼不等於當前的密碼,但我遇到了一個小問題。
發送到電子郵件喇嘛喇嘛的確認鏈接後,我有復位形式發送POST請求該方法在導軌中驗證重置密碼

def password_reset 
     @user = UserVerificationToken.token_valid(params[:format]) 
     new_pass = params[:user][:password] 
     if User.unique_reset_password(new_pass) 
      redirect_to recover_password_path :notice => "You need to choose a different password" 
     elsif @user.update_attributes(password: new_pass) 
      redirect_to root_url, :notice => "Password has been reset" 
     else 
      redirect_to recover_password_path 
     end 

    end 

和用戶模式,我有這個方法。

def self.unique_reset_password(new_pass) 
    return true unless :password == new_pass 
end 

經過10分鐘試圖找出爲什麼這不起作用,我打了我一巴掌。 Duh,new_pass還沒有被散列。
我的問題是,我該如何解決這個問題,我在正確的道路上?無論如何在將new_pass與當前散列傳遞進行比較之前先對它進行散列? 謝謝。

回答

2

嘗試通過authenticate方法:

@user = UserVerificationToken.token_valid(params[:format]) 
new_pass = params[:user][:password] 
if @user.authenticate(new_pass) 
    #no good, same as old 
    else 
    #continue 
end 

這是用在登錄(不是100%肯定,但它在我的應用程序通過RoR的微博教程建成使用)

+0

多虧沒想到的方法那我現在就試試看。 – Skyalchemist 2013-03-13 23:28:53

+0

哦哇謝謝,不會說謊我沒想到會工作。非常感謝哈哈 – Skyalchemist 2013-03-13 23:31:55

+0

你能把我鏈接到ror twitter教程嗎? – Skyalchemist 2013-03-13 23:32:40