11
votes

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

4
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
Any help or clues will be very much appreciated - alik
the Current User section in config/initializers/active_admin.rb - Tilo
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
redirect_to root_path unless warden.user.is_admin? - Tilo

4 Answers

20
votes

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

1
votes

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'
1
votes

I am not sure, but you can try something like

root :to => proc { |env| [ 302, {'Location'=> env["warden"].authenticate? ? "users/dashboard" : "/home" }, [] ] }
0
votes

What worked for me is:

constraint = lambda { |request| request.env["warden"].authenticate? and request.env['warden'].user.instance_of?(AdminUser) }