Namespaced controllers
This course reads the admin and execution routes of one Rails app and traces each to its controller file. It shows how namespace :admin do ... end adds a URL prefix and an Admin:: module, and how nested namespaces add one segment and one module level per block. It covers the module: option, scope module: combined with shallow: true, and the explicit to: "path#action" form. It also shows a three-level execution/api/v1 namespace and separates namespacing from class inheritance. A plain Ruby challenge closes it.
What you'll learn
- ✓How namespace blocks add a URL prefix and a controller module
- ✓How nested namespaces grow the URL and module one level at a time
- ✓How module: and scope module: change the controller path without changing the URL
- ✓How shallow: true drops a parent prefix from member action URLs
- ✓How to: "path#action" names a controller and action explicitly, with constraints: for filenames
- ✓Why namespace nesting and class inheritance are separate decisions
What namespace :admin do ... end generates
This app's config/routes.rb opens its admin section with:
Read the full lesson free →Curriculum
7 lessonsWhat namespace :admin do ... end generates
Shows how namespace :admin prefixes URLs with /admin and expects controllers in the Admin:: module.
Nesting namespaces: admin, then catalog, inside it
Nests catalog inside admin, adding one URL segment and one controller module level per namespace block.
The module: option: routing into a subdirectory without a URL segment
Uses module: :courses to route the cover resource to a deeper controller without changing its URL.
module: plus a shallow resource: this app's sharpest URL-vs-module split
Combines scope module: :lessons with shallow: true, so a destroy URL without lessons still reaches Lessons::ImagesController.
to: "path/to#action": no namespace or module: keyword needed
Covers to: "path#action" in the reorder routes and a lesson image route with a filename regex constraint.
Three levels deep: execution, api, v1
Traces the execution, api and v1 namespaces to Execution::Api::V1::SandboxController and separates nesting from inheritance.
Putting it together: one destination, three ways to point at it
Reviews the three routing techniques and ends with a plain Ruby challenge that builds controller class names.