仅限Safari中的Rails 3错误 - ActiveRecord :: RecordNotFound(无法找到具有auth_token =的用户):

时间:2012-02-25 16:23:33

标签: ruby-on-rails-3

在Railscast#274中获取重置密码在我的Rails 3应用程序中运行时,我在Safari中遇到了一个奇怪的问题。如果我在Heroku中运行我的应用程序,当我转到我的根目录时会出现以下错误:

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行动:

def new     @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

下面的rake任务,如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 个答案:

答案 0 :(得分:1)

  

无论何时重新生成用户:auth_code,您都需要删除该域的Cookie。在制作中,你不应该重新生成:auth_codes,除非用户编辑他们的cookie,否则你永远不会遇到这个问题。

此外,我已经在railscast.com身份验证(修订版)解决方案上发布了回复,因此Ryan可以查看它。

祝你好运!