1

我刚刚向 RailsAdmin 添加了 CanCan 授权,当我/admin以未经授权的用户身份访问时,我收到此错误:

ActionController::RoutingError at /
No route matches {:controller=>"home"}

这就是我的routes.rb样子:

MyApp::Application.routes.draw do

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

  devise_for :users, :path_names => { :sign_up => "register", 
                                      :sign_in => "login", 
                                      :sign_out => "logout",
                                                                            :settings => "settings" },
                      :controllers => {:confirmations => "confirmations"}

  devise_scope :user do
    get "login", :to => "devise/sessions#new"
    get "register", :to => "devise/registrations#new"
        get "settings", :to => "devise/registrations#edit"
    get "logout",   :to => "devise/sessions#destroy"    
  end

  get "downloads/show"
  match "downloads" => "downloads#index", via: :get

  resources :items

  devise_for :users
  resources :users

  authenticated :user do
    root :to => 'home#index'
  end
  root :to => "home#index"
end

它从我的这一点抛出错误application.rb

class ApplicationController < ActionController::Base
  protect_from_forgery

  rescue_from CanCan::AccessDenied do |exception|
    redirect_to root_path, :alert => exception.message
  end

end

一旦我转到任何其他页面并单击links_to root_path它可以工作的链接。只是当我在不允许时尝试访问 RailsAdmin 时,它才会引发此错误。

理想情况下,我希望它能够简单地按照 CanCan 所说的去做——root_path通过消息重定向到。

虽然,我想到的一件事是我正在使用 gem better_errors。这可能是造成这种情况的原因吗?

这是错误的服务器日志的完整转储:

Started GET "/admin" for 127.0.0.1 at 2013-05-08 13:59:31 -0500
Processing by RailsAdmin::MainController#dashboard as HTML
  User Load (2.1ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 18 LIMIT 1
   (0.7ms)  SELECT COUNT(*) FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = 18 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))
   (0.7ms)  SELECT COUNT(*) FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = 18 AND (((roles.name = 'seller') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))
Completed 404 Not Found in 27ms

ActionController::RoutingError - No route matches {:controller=>"home"}:
  (gem) actionpack-3.2.13/lib/action_controller/metal/exceptions.rb:11:in `initialize'
  (gem) actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:544:in `raise_routing_error'
  (gem) actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:540:in `rescue in generate'
  (gem) actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:532:in `generate'
  (gem) actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:573:in `generate'
  (gem) actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:598:in `url_for'
  (gem) actionpack-3.2.13/lib/action_dispatch/routing/url_for.rb:148:in `url_for'
  (gem) actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:220:in `root_path'
  app/controllers/application_controller.rb:5:in `block in <class:ApplicationController>'
  (gem) activesupport-3.2.13/lib/active_support/rescuable.rb:80:in `rescue_with_handler'
  (gem) actionpack-3.2.13/lib/action_controller/metal/rescue.rb:15:in `rescue_with_handler'
  (gem) actionpack-3.2.13/lib/action_controller/metal/rescue.rb:32:in `rescue in process_action'
  (gem) actionpack-3.2.13/lib/action_controller/metal/rescue.rb:29:in `process_action'
  (gem) actionpack-3.2.13/lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
  (gem) activesupport-3.2.13/lib/active_support/notifications.rb:123:in `block in instrument'
  (gem) activesupport-3.2.13/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  (gem) activesupport-3.2.13/lib/active_support/notifications.rb:123:in `instrument'
  (gem) actionpack-3.2.13/lib/action_controller/metal/instrumentation.rb:29:in `process_action'
  (gem) actionpack-3.2.13/lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
  (gem) activerecord-3.2.13/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  (gem) newrelic_rpm-3.6.1.88/lib/new_relic/agent/instrumentation/rails3/action_controller.rb:38:in `block in process_action'
  (gem) newrelic_rpm-3.6.1.88/lib/new_relic/agent/instrumentation/controller_instrumentation.rb:318:in `perform_action_with_newrelic_trace'
  (gem) newrelic_rpm-3.6.1.88/lib/new_relic/agent/instrumentation/rails3/action_controller.rb:37:in `process_action'
  (gem) actionpack-3.2.13/lib/abstract_controller/base.rb:121:in `process'
  (gem) actionpack-3.2.13/lib/abstract_controller/rendering.rb:45:in `process'
  (gem) actionpack-3.2.13/lib/action_controller/metal.rb:203:in `dispatch'
  (gem) actionpack-3.2.13/lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
  (gem) actionpack-3.2.13/lib/action_controller/metal.rb:246:in `block in action'
  (gem) actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
  (gem) actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:36:in `call'
  (gem) journey-1.0.4/lib/journey/router.rb:68:in `block in call'
  (gem) journey-1.0.4/lib/journey/router.rb:56:in `call'
  (gem) actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:612:in `call'
  (gem) railties-3.2.13/lib/rails/engine.rb:479:in `call'
  (gem) railties-3.2.13/lib/rails/railtie/configurable.rb:30:in `method_missing'
  (gem) journey-1.0.4/lib/journey/router.rb:68:in `block in call'
  (gem) journey-1.0.4/lib/journey/router.rb:56:in `call'
  (gem) actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:612:in `call'
  (gem) rack-pjax-0.7.0/lib/rack/pjax.rb:12:in `call'
  (gem) newrelic_rpm-3.6.1.88/lib/new_relic/rack/error_collector.rb:12:in `call'
  (gem) newrelic_rpm-3.6.1.88/lib/new_relic/rack/agent_hooks.rb:18:in `call'
  (gem) newrelic_rpm-3.6.1.88/lib/new_relic/rack/browser_monitoring.rb:16:in `call'
  (gem) bullet-4.6.0/lib/bullet/rack.rb:10:in `call'
  (gem) meta_request-0.2.3/lib/meta_request/middlewares/app_request_handler.rb:11:in `call'
  (gem) rack-contrib-1.1.0/lib/rack/contrib/response_headers.rb:17:in `call'
  (gem) meta_request-0.2.3/lib/meta_request/middlewares/headers.rb:16:in `call'
  (gem) meta_request-0.2.3/lib/meta_request/middlewares/meta_request_handler.rb:13:in `call'
  (gem) warden-1.2.1/lib/warden/manager.rb:35:in `block in call'
  (gem) warden-1.2.1/lib/warden/manager.rb:34:in `call'
  (gem) actionpack-3.2.13/lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
  (gem) rack-1.4.5/lib/rack/etag.rb:23:in `call'
  (gem) rack-1.4.5/lib/rack/conditionalget.rb:25:in `call'
  (gem) actionpack-3.2.13/lib/action_dispatch/middleware/head.rb:14:in `call'
  (gem) remotipart-1.0.5/lib/remotipart/middleware.rb:30:in `call'
  (gem) actionpack-3.2.13/lib/action_dispatch/middleware/params_parser.rb:21:in `call'
  (gem) actionpack-3.2.13/lib/action_dispatch/middleware/flash.rb:242:in `call'
  (gem) rack-1.4.5/lib/rack/session/abstract/id.rb:210:in `context'
  (gem) rack-1.4.5/lib/rack/session/abstract/id.rb:205:in `call'
  (gem) actionpack-3.2.13/lib/action_dispatch/middleware/cookies.rb:341:in `call'
  (gem) activerecord-3.2.13/lib/active_record/query_cache.rb:64:in `call'
  (gem) activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
  (gem) actionpack-3.2.13/lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
  (gem) activesupport-3.2.13/lib/active_support/callbacks.rb:405:in `_run__4139764133977678969__call__3020020767411987846__callbacks'
  (gem) activesupport-3.2.13/lib/active_support/callbacks.rb:405:in `__run_callback'
  (gem) activesupport-3.2.13/lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
  (gem) activesupport-3.2.13/lib/active_support/callbacks.rb:81:in `run_callbacks'
  (gem) actionpack-3.2.13/lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  (gem) actionpack-3.2.13/lib/action_dispatch/middleware/reloader.rb:65:in `call'
  (gem) actionpack-3.2.13/lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
  (gem) better_errors-0.8.0/lib/better_errors/middleware.rb:84:in `protected_app_call'
  (gem) better_errors-0.8.0/lib/better_errors/middleware.rb:79:in `better_errors_call'
  (gem) better_errors-0.8.0/lib/better_errors/middleware.rb:56:in `call'
  (gem) actionpack-3.2.13/lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
  (gem) actionpack-3.2.13/lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  (gem) railties-3.2.13/lib/rails/rack/logger.rb:32:in `call_app'
  (gem) railties-3.2.13/lib/rails/rack/logger.rb:16:in `block in call'
  (gem) activesupport-3.2.13/lib/active_support/tagged_logging.rb:22:in `tagged'
  (gem) railties-3.2.13/lib/rails/rack/logger.rb:16:in `call'
  (gem) quiet_assets-1.0.2/lib/quiet_assets.rb:18:in `call_with_quiet_assets'
  (gem) actionpack-3.2.13/lib/action_dispatch/middleware/request_id.rb:22:in `call'
  (gem) rack-1.4.5/lib/rack/methodoverride.rb:21:in `call'
  (gem) rack-1.4.5/lib/rack/runtime.rb:17:in `call'
  (gem) activesupport-3.2.13/lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  (gem) rack-1.4.5/lib/rack/lock.rb:15:in `call'
  (gem) actionpack-3.2.13/lib/action_dispatch/middleware/static.rb:63:in `call'
  (gem) railties-3.2.13/lib/rails/engine.rb:479:in `call'
  (gem) railties-3.2.13/lib/rails/application.rb:223:in `call'
  (gem) rack-1.4.5/lib/rack/content_length.rb:14:in `call'
  (gem) railties-3.2.13/lib/rails/rack/log_tailer.rb:17:in `call'
  (gem) thin-1.5.1/lib/thin/connection.rb:81:in `block in pre_process'
  (gem) thin-1.5.1/lib/thin/connection.rb:79:in `pre_process'
  (gem) thin-1.5.1/lib/thin/connection.rb:54:in `process'
  (gem) thin-1.5.1/lib/thin/connection.rb:39:in `receive_data'
  (gem) eventmachine-1.0.3/lib/eventmachine.rb:187:in `run'
  (gem) thin-1.5.1/lib/thin/backends/base.rb:63:in `start'
  (gem) thin-1.5.1/lib/thin/server.rb:159:in `start'
  (gem) rack-1.4.5/lib/rack/handler/thin.rb:13:in `run'
  (gem) rack-1.4.5/lib/rack/server.rb:268:in `start'
  (gem) railties-3.2.13/lib/rails/commands/server.rb:70:in `start'
  (gem) railties-3.2.13/lib/rails/commands.rb:55:in `block in <top (required)>'
  (gem) railties-3.2.13/lib/rails/commands.rb:50:in `<top (required)>'
  script/rails:6:in `<main>'



Started POST "/admin/__better_errors/70312574550380/variables" for 127.0.0.1 at 2013-05-08 13:59:32 -0500
4

1 回答 1

2
rescue_from CanCan::AccessDenied do |exception|
  redirect_to main_app.root_path, :alert => exception.message
end

另请参阅:https ://stackoverflow.com/a/7390376/1546887等

于 2013-05-09T13:59:57.183 回答