'Active Admin won't redirect to login for unauthorized user

When I try to access localhost:3000/admin. I just get a message "You need to sign in or sign up before continuing.". I am expecting to be redirected to the login page if I am not currently logged in. However, if I access localhost:3000/admin/login I see the login page and it works as expected.

My active_admin.rb config file is in its default state.

routes.rb

Rails.application.routes.draw do
  devise_for :admin_users, ActiveAdmin::Devise.config
  ActiveAdmin.routes(self)
end


Solution 1:[1]

Am not sure, but you could add this to your routes file just above the devise line. You might risk a redirect loop though..

get '/admin', to: redirect('/admin/dashboard')
root :to => 'admin/dashboard#index'

But it depends on how you want your application to behave. What happens if you access localhost:3000/ ?

Solution 2:[2]

I think you might have some kind of exception handler catching the exception in your ApplicationController masking the proper redirect behavior

Solution 3:[3]

I had the same error, it ended up being that I had this setup:

# config/devise.rb
config.warden do |manager|
  manager.failure_app = CustomDeviseFailureApp
end

# lib/custom_devise_failure_app.rb
class CustomDeviseFailureApp < Devise::FailureApp
  def route(scope)
    :root_url
  end
end

once i removed that, all fixed.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Magzy
Solution 3 Riley Guerin