是否有特定于Devise的方法将角色重定向到after_sign_in_path_for
返回的指定默认页面?我的应用程序控制器中有以下代码:
def after_sign_in_path_for resource
dashboard_path
end
在另一个控制器中,我不想在角色登录后访问该控制器,我有以下代码,它使用before_action
重定向角色。这段代码工作正常,但我觉得有一个更好的或Devise特定的方法来做到这一点。
before_action :redirect_user_if_signed_in
def index
end
private
def redirect_user_if_signed_in
if user_signed_in?
redirect_to dashboard_path
end
end
答案 0 :(得分:1)
我相信“设计方式”是在 config / routes.rb <中使用authenticate
,authenticated
和unauthenticated
方法/强>
unauthenticated do
get 'only-for-guests' => 'guests#index'
end
authenticated do
get 'only-for-guests' => redirect('/dashboard')
end
此处有更多示例:Define resource actions that require authentication using routes.rb