railsadminteam / rails_admin

RailsAdmin is a Rails engine that provides an easy-to-use interface for managing your data

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Redirect loop when visiting /admin

andypike opened this issue · comments

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

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'?

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.

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.

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.

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

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)

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

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

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.

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.

Awesome, thanks for posting this.

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

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