Rails namespaced route with root only works in development

namespace :admin do root :to => "admin#index" end 

I can visit localhost:3000/admin and it works. When I deploy myapp.herokuapp.com/admin to myapp.herokuapp.com/admin , no. He made it

ActionController::RoutingError (uninitialized constant Admin::AdminController):

My controller is actually an AdminController, not an Admin :: AdminController, and I'm not quite sure what the difference is or how to fix it.

Again, all this works locally.

+4
source share
2 answers

In the output of rake routes you can see that for these route routes with names, use :controller => 'admin/admin' . When it comes to defining a controller class, it converts admin/admin to Admin::AdminController . Therefore, controllers for routes with names are usually placed in the app/controllers/namespace_name directory and wrapped in the NamespaceName module. In your case, it should be the class Admin::AdminController defined in app/controllers/admin/admin_controller.rb .

Although it’s really interesting why your configuration works fine in development, it breaks down in production mode (I tried and successfully reproduced it). I believe this is related to loading and caching classes in production mode, because setting config.cache_classes = true in config/environments/development.rb causes it to break in development mode too.

And as zoltarSpeaks noted, it should be root :to => "admin#index" instead of root :to => "admin#index" .

One more note: namespaces for routes are usually used when there are several connected controllers. If you need only one AdminController , you can configure your routes as follows:

 resources :admin, :only => :index 

In this case, no other changes are required (if you want to have other default actions besides index , just remove the :only option).

+2
source

I am far from my laptop, so I can’t check, but this:

 root :to => "Admin#index" 

supposed to:

 root :to => "Admin#index" 

instead of this? It may not make any difference.

Do you have an admin folder inside controllers with an admin controller inside?

If you could show us your controller code, that would be helpful.

0
source

Source: https://habr.com/ru/post/1383813/


All Articles