2012-02-25 57 views
0

在Railscast#274中獲得重置密碼在我的Rails 3應用程序中工作,我在Safari中遇到了一個奇怪的問題。如果我在Heroku上運行我的應用程序,我得到以下錯誤,當我去我的根:Safari中的Rails 3錯誤 - ActiveRecord :: RecordNotFound(無法找到auth_token =的用戶):

ActiveRecord::RecordNotFound (Couldn't find User with auth_token =): 
app/controllers/application_controller.rb:39:in `lookup_user' 
app/controllers/application_controller.rb:32:in `current_user' 
app/controllers/application_controller.rb:54:in `logged_in?' 
app/controllers/users_controller.rb:8:in `new' 

如果使用Firefox和Chrome(在隱身模式下),它的工作原理。在Safari中,我發現如果出現錯誤,我可以通過導航到/logout使其消失。然後頁面呈現完美。

這裏是我的/logoutroot路線:

match "/logout" => "sessions#destroy", :as => "logout" 
root :to => "users#new" 

這裏是我的destroy行動sessions_controller

def destroy 
    reset_session 
    cookies.delete(:auth_token) 
    redirect_to root_path, :notice => "You successfully logged out" 
end 

application_controller

protected 

    def current_user 
    @current_user ||= lookup_user 
    end 

    def lookup_user 
    if session[:user_id] 
     User.find_by_id(session[:user_id]) 
    elsif cookies[:auth_token] 
     User.find_by_auth_token!(cookies[:auth_token]) 
    end 
    end 

最後,這裏是我的new行動users_controller

高清新 @user = User.new @ user.profile = Profile.new 如果LOGGED_IN? redirect_to的profile_path(CURRENT_USER) 結束 結束

我已經試過:

要改變new行動以下刪除Cookie:

def new 
    @user = User.new 
    @user.profile = Profile.new 
    if logged_in? 
    redirect_to profile_path(current_user) 
    elsif 
    cookies.delete(:auth_token) 
    end 
end 

摟草如下任務,如Railscast comments中建議的:

namespace :user do 
    desc "Rebuild Auth-Tokens" 
    task :rebuild_auth_token => :environment do 
    User.transaction do 
     User.all.each { |u| 
     u.generate_token(:auth_token) 
     u.save! 
     } 
    end 
    end 
end 

(I ran this with `heroku run rake user:rebuild_auth_token`) 

這兩個似乎都沒有奏效。任何人都可以幫我解決這個問題嗎?

回答

1

無論何時您重新生成用戶:auth_code's,您都需要刪除該域的Cookie。在生產中,您不應該重新生成:auth_codes,除非用戶編輯它們的cookie,否則您將永遠不會遇到此問題。

此外,我已經在railscast.com認證(修訂)解決方案上發佈了一個響應,以便Ryan可以看一下它。

祝你好運!

+0

感謝您的回覆。我在你的代碼中添加了我的'lookup'方法,它似乎使我的應用程序崩潰。我試着用/不用'return nil',並且除了'cookies.delete(:auth_token)''剝去了所有的東西',但是這同樣出現了我在上面得到的錯誤。 – tvalent2 2012-02-28 04:24:01

+0

我還添加了我嘗試運行的耙式任務。 – tvalent2 2012-02-28 04:24:19