1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ActiveAdminでログイン後に遷移するページをモデルごとに分けたい

Posted at

ActiveAdminでAdminUserとUserのようにユーザ管理を複数用意している場合、
ログイン後に遷移するトップページを分けたい時があると思います。

AdminUserでログインしたら => AdminUser向けのDashboard
Userでログインしたら => User向けのDashboard

ActiveAdminのドキュメントを読むと、

  # == Root
  #
  # Set the action to call for the root path. You can set different
  # roots for each namespace.
  #
  # Default:
  # config.root_to = 'dashboard#index'

なんて記述があるので、namespaceごとにroot_toを設定すればOKなのかな?と想像して

  config.namespace :users do |user|
    user.site_title = "ほげほげ"
    user.authentication_method = :authenticate_user!
    user.current_user_method = :current_user
    user.logout_link_path = :destroy_user_session_path
    user.root_to = '/users/dashboard#index'
  end

  config.namespace :admin do |admin|
    admin.site_title = "ほげほげ管理者画面"
    admin.authentication_method = :authenticate_admin_user!
    admin.current_user_method = :current_admin_user
    admin.logout_link_path = :destroy_admin_user_session_path
    admin.root_to = '/admin/dashboard#index'
  end

なんてやってみたのですが、全く変わらず。userでログインしてもadminに飛ばされる。。

で、調べていたら、以下の記事を見つけまして、

config/initializer/active_admin_force_redirection.rb
ActiveAdmin::Devise::SessionsController.class_eval do
  def after_sign_in_path_for(resource)
    if resource.is_a?(AdminUser)
      admin_root_path
    else
      users_root_path
    end
  end
end

こんな具合に設定したら、ちゃんと動きました。万歳!

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?