我需要在用户登录后控制重定向。我发现此问题的许多答案都指向after_sign_in_path_for方法。所以我把它添加到我的application_controller.rb:
def after_sign_in_path_for(resource_or_scope)
raise "SIGN IN TEST"
dashboard_path
end
查看其工作原理。但每当我登录时,都没有引发异常,方法只是被忽略了。我该怎样做才能使用它?
EDIT 根据要求,routes.rb的片段
devise_for :users, :controllers => {
:sessions => "user/sessions",
:registrations => "user/registrations",
:passwords => 'user/passwords'
}
答案 0 :(得分:4)
official docs for this are here
-
我们成功地使用了这个:
#app/controllers/application_controller.rb
Class ApplicationController < ActionController::Base
def after_sign_in_path_for(resource)
dashboard_path
end
end
-
如果未调用您的方法,则首先要删除RAISE
调用,然后重新启动服务器再试一次。如果这不起作用,问题可能出在Devise
如何处理您的系统
如果您可以尝试删除RAISE
&amp;让我们知道结果,这将是一个很大的帮助!
<强>更新强>
更好的是你正在使用users/sessions
控制器。这是我们的:
class Users::SessionsController < Devise::SessionsController
####################
# Redirects #
####################
protected
#Login Path (if already logged in)
def after_sign_in_path_for(resource)
dashboard_path
end
#Logout Path
def after_sign_out_path_for(resource_or_scope)
root_url
end
end