【发布时间】:2012-02-28 13:56:25
【问题描述】:
在遵循 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 使其消失。然后页面完美呈现。
这是我前往/logout 和root 的路线:
match "/logout" => "sessions#destroy", :as => "logout"
root :to => "users#new"
这是我在sessions_controller 中的destroy 操作:
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
最后,这是我在users_controller 中的new 操作:
定义新 @user = User.new @user.profile = Profile.new 如果已登录? 重定向到配置文件路径(当前用户) 结尾 结束
我的尝试:
要更改 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`)
似乎都没有奏效。谁能帮我解决这个问题?
【问题讨论】:
标签: ruby-on-rails-3