设计将用户重定向到after_sign_in_path_for的方法

时间:2016-01-17 21:26:28

标签: ruby-on-rails ruby devise

是否有特定于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

1 个答案:

答案 0 :(得分:1)

我相信“设计方式”是在 config / routes.rb <中使用authenticateauthenticatedunauthenticated方法/强>

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