简体   繁体   中英

Rails controllers in modules fail in production with ActionController::RoutingError (uninitialized constant Admin::

In our app we are using modules to organize some controllers. Here's what the directory structure looks like:

app/
  controllers/
    admin/
      products/
        reviews_controller.rb
      orders/
        line_items_controller.rb
      products_controller.rb
      orders_controller.rb

This works fine in development but is failing on our AWS server with the below error:

{"method":"GET","path":"/api/","format":"html","controller":"ApplicationController","action":"main","status":200,"duration":5.03,"view":0.15,"db":1.46,"@timestamp":"2019-02-28T16:21:08Z","process_id":36,"ip":"10.41.143.0","owner_id":null,"owner_type":null,"params":{}}

ActionController::RoutingError (uninitialized constant Admin::Products::ProductsController):
  activesupport (4.2.10) lib/active_support/inflector/methods.rb:263:in `const_get'
  activesupport (4.2.10) lib/active_support/inflector/methods.rb:263:in `block in constantize'
  activesupport (4.2.10) lib/active_support/inflector/methods.rb:259:in `each'
  activesupport (4.2.10) lib/active_support/inflector/methods.rb:259:in `inject'
  activesupport (4.2.10) lib/active_support/inflector/methods.rb:259:in `constantize'
  actionpack (4.2.10) lib/action_dispatch/routing/route_set.rb:70:in `controller_reference'
  actionpack (4.2.10) lib/action_dispatch/routing/route_set.rb:60:in `controller'
  actionpack (4.2.10) lib/action_dispatch/routing/route_set.rb:39:in `serve'
  actionpack (4.2.10) lib/action_dispatch/journey/router.rb:43:in `block in serve'
  actionpack (4.2.10) lib/action_dispatch/journey/router.rb:30:in `each'
  actionpack (4.2.10) lib/action_dispatch/journey/router.rb:30:in `serve'
  actionpack (4.2.10) lib/action_dispatch/routing/route_set.rb:817:in `call'
  rack (1.6.10) lib/rack/etag.rb:24:in `call'
  rack (1.6.10) lib/rack/conditionalget.rb:25:in `call'
  rack (1.6.10) lib/rack/head.rb:13:in `call'
  actionpack (4.2.10) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  actionpack (4.2.10) lib/action_dispatch/middleware/flash.rb:260:in `call'
  rack (1.6.10) lib/rack/session/abstract/id.rb:225:in `context'
  rack (1.6.10) lib/rack/session/abstract/id.rb:220:in `call'
  actionpack (4.2.10) lib/action_dispatch/middleware/cookies.rb:560:in `call'
  activerecord (4.2.10) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.2.10) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
  actionpack (4.2.10) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.2.10) lib/active_support/callbacks.rb:88:in `__run_callbacks__'
  activesupport (4.2.10) lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
  activesupport (4.2.10) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.10) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.2.10) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
  actionpack (4.2.10) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  actionpack (4.2.10) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  rack-cors (1.0.2) lib/rack/cors.rb:97:in `call'
  lograge (0.10.0) lib/lograge/rails_ext/rack/logger.rb:15:in `call_app'
  railties (4.2.10) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.2.10) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.2.10) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.2.10) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.2.10) lib/rails/rack/logger.rb:20:in `call'
  request_store (1.4.1) lib/request_store/middleware.rb:19:in `call'
  actionpack (4.2.10) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.6.10) lib/rack/methodoverride.rb:22:in `call'
  rack (1.6.10) lib/rack/runtime.rb:18:in `call'
  activesupport (4.2.10) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  actionpack (4.2.10) lib/action_dispatch/middleware/static.rb:120:in `call'
  rack (1.6.10) lib/rack/sendfile.rb:113:in `call'
  ddtrace (0.8.0) lib/ddtrace/contrib/rack/middlewares.rb:89:in `call'
  sentry-raven (2.7.4) lib/raven/integrations/rack.rb:51:in `call'
  railties (4.2.10) lib/rails/engine.rb:518:in `call'
  railties (4.2.10) lib/rails/application.rb:165:in `call'
  rack (1.6.10) lib/rack/urlmap.rb:66:in `block in call'
  rack (1.6.10) lib/rack/urlmap.rb:50:in `each'
  rack (1.6.10) lib/rack/urlmap.rb:50:in `call'
  puma (3.12.0) lib/puma/configuration.rb:225:in `call'
  puma (3.12.0) lib/puma/server.rb:658:in `handle_request'
  puma (3.12.0) lib/puma/server.rb:472:in `process_client'
  puma (3.12.0) lib/puma/server.rb:332:in `block in run'
  puma (3.12.0) lib/puma/thread_pool.rb:133:in `block in spawn_thread'

  namespace :admin, shallow: true do

     resources :products, module: 'products' do
       resources :reviews
     end
     resources :orders, module: 'orders' do
       resources :line_items
     end

  end


I'm not sure why it's looking for Admin::Products::ProductsController, because the products controller is under /app/controllers/admin/ , not app/controllers/admin/products/ .

Would really appreciate some help with this. I'm having trouble understanding why this is only occurring in production.

rake routes | grep 'products' rake routes | grep 'products' This is as much as I can share at the moment:

admin_products GET      /admin/products(.:format)                                                           admin/products/products#index
POST     /admin/products(.:format)                                                           admin/products/products#create
new_admin_products GET      /admin/products/new(.:format)                                                       admin/products/products#new
edit_admin_products GET      /admin/products/:id/edit(.:format)                                                  admin/products/products#edit
admin_product GET      /admin/products/:id(.:format)                                                       admin/products/products#show
PATCH    /admin/products/:id(.:format)                                                       admin/products/products#update
PUT      /admin/products/:id(.:format)                                                       admin/products/products#update
DELETE   /admin/products/:id(.:format)                                                       admin/products/products#destroy

The issue is that you are defining your module on the resources :products . You should define your module on the resources :reviews like this:

resources :products do
  resources :reviews, module: 'products'
end

When you give a route a module , it will look inside ModuleName::ControllerName for the action(s).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM