如何调整ActiveAdmin登录后的重定向至admin/dashboard?
Hey there, let's work through this ActiveAdmin redirect issue—this is a common gotcha, so I’ve got a few targeted fixes to try:
1. Check Devise’s After-Login Redirect Logic
The most likely culprit is a custom after_sign_in_path_for method in your application controller that’s overriding ActiveAdmin’s default behavior. If you’ve added this method for regular users, you need to make it distinguish between admin users and regular users.
Open app/controllers/application_controller.rb and update the method (or add it if it’s missing):
def after_sign_in_path_for(resource) # Redirect admin users to the dashboard, regular users to root if resource.is_a?(AdminUser) admin_dashboard_path else root_path end end
This ensures admins get sent to the correct admin dashboard after login, while regular users still hit the root path as expected.
2. Verify ActiveAdmin’s Root Configuration
Double-check that your ActiveAdmin initializer isn’t misconfigured. Open config/initializers/active_admin.rb and confirm this line exists (it’s the default, but sometimes gets modified):
config.root_to = 'dashboard#index'
This tells ActiveAdmin where to send authenticated admins by default.
3. Fix Your Load Paths Modification
Your change to config.load_paths might be causing ActiveAdmin to miss critical files. Instead of replacing the default load paths, append your custom directory (if you need it):
# config/application.rb config.load_paths += [File.join(Rails.root, "admin", "dashboard")]
Overwriting the entire load path with just the dashboard directory can prevent ActiveAdmin from loading other admin resources or core config. Also, make sure your dashboard file exists at app/admin/dashboard.rb (the default location) with the correct structure:
# app/admin/dashboard.rb ActiveAdmin.register_page "Dashboard" do menu priority: 1, label: proc{ I18n.t("active_admin.dashboard") } content title: proc{ I18n.t("active_admin.dashboard") } do # Your dashboard content here end end
4. Validate Your Routes
Ensure your config/routes.rb has the ActiveAdmin routes declared correctly, and no conflicting Devise routes are overriding them:
# Make sure this line is present (usually at the top of routes.rb) ActiveAdmin.routes(self) # Devise for admin users should look like this (no custom redirects unless intentional) devise_for :admin_users, ActiveAdmin::Devise.config
5. Clear Cache & Restart
Rails cache can sometimes hold onto old route or config data. Run this command to clear temporary files, then restart your server:
rails tmp:clear
Try these steps in order—start with the after_sign_in_path_for fix, since that’s the most frequent cause of this exact issue. Let me know if any of these get you back to the admin dashboard!
内容的提问来源于stack exchange,提问作者Jason Harder

