11

Active Admin is a gem used for having an admin dashboard in your application. It uses Devise for logging in users and creates a separate admin_user model for the admins. My application already uses devise and has its users as the user model. Ever since I started using the active admin gem, in my routes file the following line keeps resolving to home#index and not users#dashboard even when my user is logged in. This used to work fine earlier where logged in users were taken to users#dashboard as the root url.

root :to => 'users#dashboard', :constraints => lambda {|r| r.env["warden"].authenticate? }
root :to => 'home#index'

What is happening is that the .authenticate? is checking for the admin_user (belonging to Active Admin) being logged in or not but not my user model which is what I need to check for, so when I am logged in to active admin interface, my site root becomes users#dashboard instead without checking if the user is logged in or not. How can I make .authenticate? check for the user being logged in and not admin_user ?

Any help or clues will be very much appreciated

alik
  • 3,820
  • 9
  • 41
  • 55
  • It actually seems like I am getting this issue due to Active Admin and the fact that it uses Devise. Logging in to the active admin interface affects the behavior such that the app attempts to take every user to the dashboard if the active admin is logged in and every user to the home#index if active admin is logged out, both regardless of if the general user itself is logged in or not – alik Oct 03 '11 at 20:00
  • Any help or clues will be very much appreciated – alik Oct 04 '11 at 00:31
  • the Current User section in config/initializers/active_admin.rb – Tilo Oct 04 '11 at 01:07
  • Actually that is already set to `config.current_user_method = :current_admin_user` which is different from `current_user` used throughout the app. Thing is, Devise uses Warden, and the command above I am using in the Routes file is dependent on Warden. If there was another command I could use which was more specific to which user to use, it would make it so much easier :/ – alik Oct 04 '11 at 01:13
  • redirect_to root_path unless warden.user.is_admin? – Tilo Oct 04 '11 at 01:20
  • trying to figure the syntax for that. but very likely that devise's scope feature needs to be utilized here – alik Oct 04 '11 at 01:45

4 Answers4

20

I was able to solve this. The issue was related to Devise expecting a single user model in the application. Here is how to fix it.

In the config/initializers/devise.rb file, add:

config.scoped_views = true

and

config.default_scope = :user #or whichever is your regular default user model

thats it, warden checks the :user for being logged in and not :admin_user

alik
  • 3,820
  • 9
  • 41
  • 55
1

Why are you using the get "/"? You should remove it. I'm using a definition pretty similar to yours and works fine with me. Use just:

root :to => 'users#dashboard', :constraints => lambda {|r| r.env["warden"].authenticate? }
root :to => 'home#index'
lucapette
  • 20,564
  • 6
  • 65
  • 59
  • I tried that too, but it still always takes me to home#index. It seems like `lambda {|r| r.env["warden"].authenticate? }` always returns false for me regardless of the fact the the user is logged in or not. – alik Oct 03 '11 at 19:47
1

I am not sure, but you can try something like

root :to => proc { |env| [ 302, {'Location'=> env["warden"].authenticate? ? "users/dashboard" : "/home" }, [] ] }
fl00r
  • 82,987
  • 33
  • 217
  • 237
  • I tried using `root :to => proc { |env| [ 302, {'Location'=> env["warden"].authenticate? ? "users/dashboard" : "/home/index" }, [] ] }` but it tries to always take me to /home/index regardless of the fact that the user is logged in or not. Im suspecting something is up with the authenticate? command, maybe always resulting in false – alik Oct 03 '11 at 19:38
  • I am not familiar with Warden, but you can look deep into it's source to implement `authenticate?` in your proc. Or `env["warden"]` returns not what expected. – fl00r Oct 03 '11 at 19:48
0

What worked for me is:

constraint = lambda { |request| request.env["warden"].authenticate? and request.env['warden'].user.instance_of?(AdminUser) }
Chris Edwards
  • 3,514
  • 2
  • 33
  • 40