Redirect loop when visiting /admin #715

Closed
andypike opened this Issue Sep 13, 2011 · 13 comments

Comments

Projects
None yet
8 participants
@andypike

Hi all,

I've just installed rails_admin for the first time and have hit a bit of a snag. After installation I go to /admin and I end up in a redirection loop. When Chrome displays the error the url in the address bar is: /admin_users/sign_in

Firstly, during the install I had a problem with Devise. I think the gem 'devise' line was added to the end of my gemfile but without a new line and so it was appended to and closing block end (eg: endgem 'devise') which caused problems for the rails_admin install process. I got through that and updated devise manually afterwards.

I'm also user a custom user model of admin_user as I already have a user model that I don't want to allow access to rails_admin.

I haven't done any other customization from the default install. But I get the redirection loop if I go to /admin. I've checked over everything and it all looks good (as far as I can tell).

I can get it to work if I edit the rails_admin initializer to include an empty config.authenticate_with block. But of course this means there isn't any authentication and anyone can see the admin panel.

Any ideas to point me in the right direction? This might be related to the failed initial install but I think I covered everything.

Thanks in advance

Andy

@bbenezech

This comment has been minimized.

Show comment Hide comment
@bbenezech

bbenezech Sep 14, 2011

Collaborator

You just need to set current_user_method so that RA knows which user to use, that's all. Check your initializer.
You get a redirection loop maybe because your root url isn't set or is set to /admin?

Can you give me your Gemfile, so I can see why Thor screwed up with the gem 'devise'?

Collaborator

bbenezech commented Sep 14, 2011

You just need to set current_user_method so that RA knows which user to use, that's all. Check your initializer.
You get a redirection loop maybe because your root url isn't set or is set to /admin?

Can you give me your Gemfile, so I can see why Thor screwed up with the gem 'devise'?

@andypike

This comment has been minimized.

Show comment Hide comment
@andypike

andypike Sep 14, 2011

Hi, thanks for the fast reply!

This is setup in my initializer:

config.current_user_method { current_admin_user }.

Do I need to implement the current_admin_user method? I thought that was supplied by devise but I might be mistaken? Also, I do have a root_url setup, here is a stripped down routes.rb that has the same problem:

Brewster::Application.routes.draw do  
  mount RailsAdmin::Engine => '/admin', :as => 'rails_admin'
  devise_for :admin_users

  root :to => 'pages#home'
end

Regarding the gemfile, here it is with the devise reference moved down onto a newline:

source 'http://rubygems.org'

gem 'rails', '3.1.0'
gem 'simple_form'
gem 'haml-rails'
gem 'jquery-rails'
gem 'cancan'
gem 'rails_admin', :git => 'git://github.com/sferik/rails_admin.git'

group :assets do
  gem 'sass-rails', "  ~> 3.1.0"
  gem 'coffee-rails', "~> 3.1.0"
  gem 'uglifier'
end

group :test do
  gem 'turn', :require => false
  gem 'cucumber-rails'
  gem 'database_cleaner'
  gem 'shoulda'
  gem 'launchy'
  gem 'simplecov', '>= 0.4.0', :require => false
end

group :test, :development do
  gem "rspec-rails"
  gem 'sqlite3'
  gem 'fabrication'
end

group :development do
  gem 'pry' # To use pry with the rails console use: $ pry -r ./config/environment
            # To debug, add the following in source code: 
            # => binding.pry 
            # then go to the webserver terminal (see http://railscasts.com/episodes/280-pry-with-rails)
end
gem "devise"

Any thing I can try to debug the problem? Sorry for being a n00b.

Hi, thanks for the fast reply!

This is setup in my initializer:

config.current_user_method { current_admin_user }.

Do I need to implement the current_admin_user method? I thought that was supplied by devise but I might be mistaken? Also, I do have a root_url setup, here is a stripped down routes.rb that has the same problem:

Brewster::Application.routes.draw do  
  mount RailsAdmin::Engine => '/admin', :as => 'rails_admin'
  devise_for :admin_users

  root :to => 'pages#home'
end

Regarding the gemfile, here it is with the devise reference moved down onto a newline:

source 'http://rubygems.org'

gem 'rails', '3.1.0'
gem 'simple_form'
gem 'haml-rails'
gem 'jquery-rails'
gem 'cancan'
gem 'rails_admin', :git => 'git://github.com/sferik/rails_admin.git'

group :assets do
  gem 'sass-rails', "  ~> 3.1.0"
  gem 'coffee-rails', "~> 3.1.0"
  gem 'uglifier'
end

group :test do
  gem 'turn', :require => false
  gem 'cucumber-rails'
  gem 'database_cleaner'
  gem 'shoulda'
  gem 'launchy'
  gem 'simplecov', '>= 0.4.0', :require => false
end

group :test, :development do
  gem "rspec-rails"
  gem 'sqlite3'
  gem 'fabrication'
end

group :development do
  gem 'pry' # To use pry with the rails console use: $ pry -r ./config/environment
            # To debug, add the following in source code: 
            # => binding.pry 
            # then go to the webserver terminal (see http://railscasts.com/episodes/280-pry-with-rails)
end
gem "devise"

Any thing I can try to debug the problem? Sorry for being a n00b.

@pollingj

This comment has been minimized.

Show comment Hide comment
@pollingj

pollingj Sep 16, 2011

I'm also having this issue with a fresh install. My model is called 'admin', but other than that I'm exactly the same as above.

I'm also having this issue with a fresh install. My model is called 'admin', but other than that I'm exactly the same as above.

@pollingj

This comment has been minimized.

Show comment Hide comment
@pollingj

pollingj Sep 17, 2011

Ok, think I've spotted it. When I had the Rails::Engine pointing towards '/admin' and the devise_for :admins the model the route it was attempting to go to was /admins/sign_in which was trying to hit this

Processing by RailsAdmin::MainController#show as HTML Parameters: {"model_name"=>"s", "id"=>"sign_in"}

So it looks like Rails Admin is greedily grabbing anything with the word /admin in it's path. As soon as I changed the rails admin path to /cms everything worked fine.

Ok, think I've spotted it. When I had the Rails::Engine pointing towards '/admin' and the devise_for :admins the model the route it was attempting to go to was /admins/sign_in which was trying to hit this

Processing by RailsAdmin::MainController#show as HTML Parameters: {"model_name"=>"s", "id"=>"sign_in"}

So it looks like Rails Admin is greedily grabbing anything with the word /admin in it's path. As soon as I changed the rails admin path to /cms everything worked fine.

@bbenezech

This comment has been minimized.

Show comment Hide comment
@bbenezech

bbenezech Sep 17, 2011

Collaborator

What if you put RA route after devise :admins?
Is it the same?

Collaborator

bbenezech commented Sep 17, 2011

What if you put RA route after devise :admins?
Is it the same?

@andypike

This comment has been minimized.

Show comment Hide comment
@andypike

andypike Sep 17, 2011

I just tried out the suggestion by @pollingj and that works great. I spotted a similar message in the server logs but mine was:

Processing by RailsAdmin::MainController#show as HTML
Parameters: {"model_name"=>"_users", "id"=>"sign_in"} 

I assumed the _users was how rails_admin worked. Anyway, I've also moved the RA route so it's under the devise route and that also works with /admin.

Thanks guys for the help :o)

I just tried out the suggestion by @pollingj and that works great. I spotted a similar message in the server logs but mine was:

Processing by RailsAdmin::MainController#show as HTML
Parameters: {"model_name"=>"_users", "id"=>"sign_in"} 

I assumed the _users was how rails_admin worked. Anyway, I've also moved the RA route so it's under the devise route and that also works with /admin.

Thanks guys for the help :o)

@bbenezech

This comment has been minimized.

Show comment Hide comment
@bbenezech

bbenezech Sep 17, 2011

Collaborator

Ok, not a RA issue, then.. Just the way Rack::Mount works. I thought it would match /admin/ instead of /admin...

Collaborator

bbenezech commented Sep 17, 2011

Ok, not a RA issue, then.. Just the way Rack::Mount works. I thought it would match /admin/ instead of /admin...

@bbenezech bbenezech closed this Sep 17, 2011

@iRonin

This comment has been minimized.

Show comment Hide comment
@iRonin

iRonin Jun 4, 2012

I've just run with the same issue when using admin model. If I put devise_for :admin before RA it works.

iRonin commented Jun 4, 2012

I've just run with the same issue when using admin model. If I put devise_for :admin before RA it works.

@softpro

This comment has been minimized.

Show comment Hide comment
@softpro

softpro Jan 20, 2013

Contributor

I have this in routes.rb:

    devise_for :users
    devise_for :staff,     :class_name => 'User::Staff'
    devise_for :customers, :class_name => 'User::Customer'

    mount RailsAdmin::Engine => '/admin', :as => 'rails_admin'

User::* descend from User.

How to use User::Staff for RA? Now /admin redirects to / on 401 Unauthorized.

Contributor

softpro commented Jan 20, 2013

I have this in routes.rb:

    devise_for :users
    devise_for :staff,     :class_name => 'User::Staff'
    devise_for :customers, :class_name => 'User::Customer'

    mount RailsAdmin::Engine => '/admin', :as => 'rails_admin'

User::* descend from User.

How to use User::Staff for RA? Now /admin redirects to / on 401 Unauthorized.

@softpro

This comment has been minimized.

Show comment Hide comment
@softpro

softpro Jan 20, 2013

Contributor

Got it! Should use this code:

    devise_for :staff,     :class_name => 'User::Staff'
    devise_for :customers, :class_name => 'User::Customer'
    devise_for :users

    mount RailsAdmin::Engine => '/admin', :as => 'rails_admin'

RA uses the first devise_for. But maybe Devise itself behaves so, not RA.

Contributor

softpro commented Jan 20, 2013

Got it! Should use this code:

    devise_for :staff,     :class_name => 'User::Staff'
    devise_for :customers, :class_name => 'User::Customer'
    devise_for :users

    mount RailsAdmin::Engine => '/admin', :as => 'rails_admin'

RA uses the first devise_for. But maybe Devise itself behaves so, not RA.

@pehrlich

This comment has been minimized.

Show comment Hide comment
@pehrlich

pehrlich Feb 13, 2013

Awesome, thanks for posting this.

Awesome, thanks for posting this.

@BartlomiejSkwira

This comment has been minimized.

Show comment Hide comment
@BartlomiejSkwira

BartlomiejSkwira Mar 11, 2013

Suffered from same problem, @iRonin's simple solution to switch lines helped me. I've added this info to the Troubleshooting wiki

Suffered from same problem, @iRonin's simple solution to switch lines helped me. I've added this info to the Troubleshooting wiki

@kidlab

This comment has been minimized.

Show comment Hide comment
@kidlab

kidlab Mar 11, 2015

Yeah, I got this problem too. @iRonin's solution works! Thank you.

kidlab commented Mar 11, 2015

Yeah, I got this problem too. @iRonin's solution works! Thank you.

@dylanmmatthews dylanmmatthews referenced this issue in dylanmmatthews/fantasySportsApp Jan 19, 2017

Open

Create Log in page when accessing /admin #7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment