Devise + Active Admin Redirect
Asked Answered
D

2

6

I am having trouble setting up the redirect for my application. Users should go to their profile (users/show) and admins should go to the admin dashboard.. How do i set this up?

Currently getting the following error:

 NameError in ActiveAdmin::Devise::SessionsController#create

    undefined local variable or method `admin' for #<ActiveAdmin::Devise::SessionsController:0x007febe12667e8>

Application controller

def after_sign_in_path_for(resource_or_scope)
   if admin
   redirect_to admin_dashboard_path
  else
   @user
  end
 end
end
Defensive answered 8/7, 2012 at 17:48 Comment(0)
S
15

You don't have an admin variable to access, you need to check what the parameter is that you are being given.

def after_sign_in_path_for(resource)
  stored_location_for(resource) ||
    if resource.is_a?(Admin)
      admin_dashboard_path
    else
      user_path(resource)
    end
end

You should also not redirect inside this method, it should only return a path that devise can use.

Starter answered 8/7, 2012 at 17:58 Comment(3)
The default setup with rails g active_admin:install creates an AdminUser class, so I had to modify this to if resource.is_a?(AdminUser).Gyn
Damn, this probably just saved me hours and hours of head-scratching. Thanks a lot!Checkrein
Me too! Thanks for taking the time to answer this question. (mine needed to be AdminUser as well)Huberman
W
0
if resource.class == User
  root_path
elsif resource.class == AdminUser
  admin_root_path
else
end
Wilmoth answered 27/7, 2017 at 23:14 Comment(1)
While this code may answer the question, it would be better to explain how it solves the problem without introducing others and why to use it. Code-only answers are not useful in the long run. Also, make sure to format your code at the top.Overanxious

© 2022 - 2024 — McMap. All rights reserved.