rails_admin working in development but not in production or heroku
I have a very basic Rails 3.1rc6 application with basically only Devise, CanCan and rails_admin on it, and a handful of very simple models.
In development environment rails_admin works fine, I can hit localhost:3000/admin
and after logging in with my administrator user I can see all the data and manage it.
But when I deploy to Heroku and try to hit appname.herokuapp.com/admin
I get "The page you were looking for doesn't exist. You may have mistyped the address or the page may have moved". This is the tail from Heroku logs:
app[web.1]:
app[web.1]:
app[web.1]: Started GET "/admin" for 79.157.xx.xx at 2011-08-24 12:15:52 +0000
app[web.1]:
app[web.1]: ActionController::RoutingError (No route matches {:controller=>"home"}):
heroku[router]: GET appname.herokuapp.com/admin dyno=web.1 queue=0 wait=0ms service=7ms status=404 bytes=728
app[web.1]: app/controllers/application_controller.rb:5:in `block in <class:ApplicationController>'
app[web.1]:
app[web.1]:
app[web.1]: cache: [GET /admin] miss
app[web.1]: Processing by RailsAdmin::MainController#index as HTML
app[web.1]: Completed 404 Not Found in 2ms
All other routes (Devise routes basically) work fine.
When running Thin in production (webserver that I use in Heroku) in my local machine I get the same:
$ bundle exec rails server thin -e production
=> Booting Thin
=> Rails 3.1.0.rc6 application starting in production on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Sprockets::Environment#static_root is deprecated
>> Thin web server (v1.2.11 codename Bat-Shit Crazy)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:3000, CTRL+C to stop
cache: [GET /admin] miss
Started GET "/admin" for 127.0.0.1 at 2011-08-24 13:49:42 +0200
Processing by RailsAdmin::MainController#index as HTML
Completed 404 Not Found in 3ms
ActionController::RoutingError (No route matches {:controller=>"home"}):
app/controllers/application_controller.rb:5:in `block in <class:ApplicationController>'
Rendered /home/mccoy/.rvm/gems/ruby-1.9.2-p290@talleres-r31/gems/actionpack-3.1.0.rc6/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.9ms)
And on the browser
Routing Error
No route matches {:controller=>"home"}
This is my routes.rb
root :to => "home#index"
devise_for :users
resources :users, :only => :show
mount RailsAdmin::Engine => '/admin', :as => 'rails_admin'
initializers/rails_admin.rb
RailsAdmin.config do |config|
config.authorize_with :cancan
config.reload_between_requests = false
end
Here the routes on heroku
$ heroku run rake routes
Running rake routes attached to terminal... up, run.8
root / {:controller=>"home", :action=>"index"}
new_user_session GET /users/sign_in(.:format) {:action=>"new", :controller=>"devise/sessions"}
user_session POST /users/sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"}
destroy_user_session GET /users/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
user_password POST /users/password(.:format) {:action=>"create", :controller=>"devise/passwords"}
new_user_password GET /users/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"}
edit_user_password GET /users/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
PUT /users/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
cancel_user_registration GET /users/cancel(.:format) {:action=>"cancel", :controller=>"devise/registrations"}
user_registration POST /users(.:format) {:action=>"create", :controller=>"devise/registrations"}
new_user_registration GET /users/sign_up(.:format) {:action=>"new", :controller=>"devise/registrations"}
edit_user_registration GET /users/edit(.:format) {:action=>"edit", :controller=>"devise/registrations"}
PUT /users(.:format) {:action=>"update", :controller=>"devise/registrations"}
DELETE /users(.:format) {:action=>"destroy", :controller=>"devise/registrations"}
user GET /users/:id(.:format) {:action=>"show", :controller=>"users"}
rails_admin /admin {:to=>RailsAdmin::Engine}
And finally here local routes... the same!
$ rake routes
root / {:controller=>"home", :action=>"index"}
new_user_session GET /users/sign_in(.:format) {:action=>"new", :controller=>"devise/sessions"}
user_session POST /users/sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"}
destroy_user_session GET /users/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
user_password POST /users/password(.:format) {:action=>"create", :controller=>"devise/passwords"}
new_user_password GET /users/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"}
edit_user_password GET /users/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
PUT /users/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
cancel_user_registration GET /users/cancel(.:format) {:action=>"cancel", :controller=>"devise/registrations"}
user_registration POST /users(.:format) {:action=>"create", :controller=>"devise/registrations"}
new_user_registration GET /users/sign_up(.:format) {:action=>"new", :controller=>"devise/registrations"}
edit_user_registration GET /users/edit(.:format) {:action=>"edit", :controller=>"devise/registrations"}
PUT /users(.:format) {:action=>"update", :controller=>"devise/registrations"}
DELETE /users(.:format) {:action=>"destroy", :controller=>"devise/registrations"}
user GET /users/:id(.:format) {:action=>"show", :controller=>"users"}
rails_admin /admin {:to=>RailsAdmin::Engine}
Any idea on why the rails_admin routes work in development but not in production and what can I do to fix this?
In case it helps, when I run WEBrick in production it fails with
$ rails s production
Exiting
/home/mccoy/.rvm/gems/ruby-1.9.2-p290@talleres-r31/gems/activesupport-3.1.0.rc6/lib/active_support/dependencies.rb:237:in `require': no such file to load -- rack/handler/production (LoadError)
from /home/mccoy/.rvm/gems/ruby-1.9.2-p290@talleres-r31/gems/activesupport-3.1.0.rc6/lib/active_support/dependencies.rb:237:in `block in require'
from /home/mccoy/.rvm/gems/ruby-1.9.2-p290@talleres-r31/gems/activesupport-3.1.0.rc6/lib/active_support/dependencies.rb:223:in `block in load_dependency'
from /home/mccoy/.rvm/gems/ruby-1.9.2-p290@talleres-r31/gems/activesupport-3.1.0.rc6/lib/active_support/dependencies.rb:636:in `new_constants_in'
from /home/mccoy/.rvm/gems/ruby-1.9.2-p290@talleres-r31/gems/activesupport-3.1.0.rc6/lib/active_support/dependencies.rb:223:in `load_dependency'
from /home/mccoy/.rvm/gems/ruby-1.9.2-p290@talleres-r31/gems/activesupport-3.1.0.rc6/lib/active_support/dependencies.rb:237:in `require'
from /home/mccoy/.rvm/gems/ruby-1.9.2-p290@talleres-r31/gems/rack-1.3.2/lib/rack/handler.rb:63:in `try_require'
from /home/mccoy/.rvm/gems/ruby-1.9.2-p290@talleres-r31/gems/rack-1.3.2/lib/rack/handler.rb:16:in `get'
from /home/mccoy/.rvm/gems/ruby-1.9.2-p290@talleres-r31/gems/rack-1.3.2/lib/rack/server.rb:269:in `server'
from /home/mccoy/.rvm/gems/ruby-1.9.2-p290@talleres-r31/gems/railties-3.1.0.rc6/lib/rails/commands/server.rb:59:in `start'
from /home/mccoy/.rvm/gems/ruby-1.9.2-p290@talleres-r31/gems/railties-3.1.0.rc6/lib/rails/commands.rb:54开发者_StackOverflow社区:in `block in <top (required)>'
from /home/mccoy/.rvm/gems/ruby-1.9.2-p290@talleres-r31/gems/railties-3.1.0.rc6/lib/rails/commands.rb:49:in `tap'
from /home/mccoy/.rvm/gems/ruby-1.9.2-p290@talleres-r31/gems/railties-3.1.0.rc6/lib/rails/commands.rb:49:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
But I think this is more of a gem / config problem on my side than anything related. Works fine with development environment though.
After further (and very painful) investigation, I discovered this was caused by cancan and not rails_admin.
The test user in the production database didn't have the right role assigned, so it was not authorized to access rails_admin. This threw the corresponding AccessDenied exception which was handled in application_controller.rb by
rescue_from CanCan::AccessDenied do |exception|
redirect_to root_url, :alert => exception.message
end
But for some reason redirect_to root_url
fails with the aforementioned routing error. Seems I'm not the only one with this problem, after reading this open issue https://github.com/ryanb/cancan/issues/443
The test user in the development database had the right role and could access rails_admin correctly, which made debugging this incredibly frustrating.
At least now I'm in the right path!
精彩评论