Proposal to add debugging guidance to "Routing from the outside in" for nested controllers and views

The Rails documentation really doesn’t discuss nested controllers and views in any of the top three places you’d look for it (“Routing from the outside in”, “Action View Overview”, or “Action Controller Overview”).

I propose to add to the “Routing from the Outside In” to help with Rails errors that I think become especially opaque in the context of nested controllers or views. Many a time have I been staring at the thing saying to myself, “I can see it right there!” But it’s in the wrong place.

I am not attempting to edit the actual Rails docs because I may have made an error here. Corrections or feedback welcome.

No route matches [GET] “/folder_a”

If you’re using nested controllers, you aren’t ready to solve this error yet. Read this first:

<Controller#method> is missing a template for request formats: text/html

The least important part of this error is the wording about the format, because for simple applications and Rails 7 hotwire your only format is html. This error is meant to focus you on finding the view template file expected by the controller method. If your controller is namespaced/nested, that view must be in a parallel, also-nested subfolder.

# a controller in this folder
- app/controllers/folder_a/xyz_controller.rb

# with a method
class FolderA::XyzController < ApplicationController
def show
end

# expects a view in

- app/views/folder_a/xyz/show.<whatever>

uninitialized constant You can see the controller right there! Check the nesting.

# A controller set up like this:
 - folder_a/xyz_controller.rb

# expects a controller name like this:
class FolderA::XyzController < ApplicationController
...

This error triggers if the routes are incorrectly specified. Given that controller name above, this route is wrong and will trigger that error:

# routes.rb
get :show_something, to: 'xyz#show_something'

It should be:

# routes.rb
get :show_something, to: 'folder_a/xyz#show_something'

Lastly, this error triggers if you attempt to define a resource (singular, no id required) and do not exclude the autogenerated routes:

# routes.rb

# this triggers the error when you visit /folder_a
# because it is looking for an FolderAController#index
resource :folder_a

# to eliminate the error when someone visits /folder_a, specify a 
# non-nested controller and define an index action. this is fine:
/app/controllers/folder_a_controller.rb
# (the index action is not shown; write it)
# and our previous nested controller from before:
/app/controllers/folder_a/xyz_controller.rb

This approach excludes the autogenerated routes and does not trigger this error:

# routes.rb
# this does not trigger this exact error when you visit /folder_a
# because it does not look for #index; 
# if you have nothing but this line (no nested routes, you will 
# get "No route matches" below when you visit /folder_a)
resource :folder_a, only: []

No route matches [GET] “/folder_a”

Now that you know the above, this error will be solvable. You need a controller with appropriately nested folder and file names and matching nested class names. You need views with appropriately nested folder and file names. Your routes must reflect this nesting.