在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"
以下是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`)
似乎都没有效果。任何人都可以帮我解决这个问题吗?
答案 0 :(得分:1)
无论何时重新生成用户:auth_code,您都需要删除该域的Cookie。在制作中,你不应该重新生成:auth_codes,除非用户编辑他们的cookie,否则你永远不会遇到这个问题。
此外,我已经在railscast.com身份验证(修订版)解决方案上发布了回复,因此Ryan可以查看它。
祝你好运!