In config/routes.rb, how do namespace :admin, scope module: :admin, scope path: and scope as: differ in URL, helper name and controller class?
answer
- three axes: path, name, class
- namespace sets all three
- module: moves only the class
- path: moves only the URL
- as: renames only helpers
basics
~10 snamespace :admin prefixes the URL (/admin/stories), the helpers (admin_stories_path) and the controller (Admin::StoriesController) at once; scope module: changes only the class, scope path: only the URL, and scope as: only the helper names.
solid answer
~30 sEvery route has a URL, a route name behind its helpers and a controller class. `namespace :admin` changes all three: `resources :stories` inside it serves `/admin/stories`, names `admin_stories_path` and dispatches to `Admin::StoriesController` in `app/controllers/admin/`. `scope` changes only what you pass it: `scope module: :admin` gives `Admin::StoriesController` at `/stories` with `stories_path`; `scope path: "/admin"` (or `scope "admin"`) gives `/admin/stories` with `StoriesController` and `stories_path`; `scope as: "admin"` gives `admin_stories_path` for `/stories` and `StoriesController`. A `namespace` also accepts `path:` and `as:` to override its URL or helper prefix while keeping the module.
code
ruby · 14 lines# config/routes.rb
Rails.application.routes.draw do
resources :stories, only: [:index, :show]
namespace :admin do
resources :stories do
post :publish, on: :member
end
end
end
# GET /stories/:id -> stories#show story_path(story)
# GET /admin/stories/:id -> admin/stories#show admin_story_path(story)
# POST /admin/stories/:id/publish -> admin/stories#publish publish_admin_story_path(story)go deeper
Know that namespace :admin gives /admin URLs, admin_ helpers and Admin:: controllers in app/controllers/admin.
Fill in the table for namespace, scope module:, scope path: and scope as:, and combine options to change exactly the axis you need.
Choose the grouping that matches how the code is organised and how URLs must look, and avoid helper collisions when two route sets share resources.
Agree conventions for areas like admin and API sections so URL, helper and module names stay predictable across teams.
## Three things every route has A newsroom CMS has a public site and an editors' back office. Both work with stories, so `config/routes.rb` needs two sets of story routes that do not collide. Rails groups routes with `namespace` and `scope`, and the difference between them is clearest once you see that every route has **three independent names**: - the **URL** a browser requests, such as `/admin/stories`; - the **route name** behind the helpers, such as `admin_stories` for `admin_stories_path`; - the **controller class** that handles the request, such as `Admin::StoriesController`. ## namespace changes all three ```ruby namespace :admin do resources :stories end ``` | Route | URL | Helper | Controller | |---|---|---|---| | index | `/admin/stories` | `admin_stories_path` | `Admin::StoriesController` | | show | `/admin/stories/:id` | `admin_story_path(story)` | `Admin::StoriesController` | The controller lives in `app/controllers/admin/stories_controller.rb`, and Zeitwerk maps that directory to the `Admin` module. This is the standard shape for a back office: separate URLs, separate helpers, separate controllers with their own filters. ## scope changes only what you ask `scope` is the general tool; `namespace` is a preset of it. Each option moves one axis: | Block | URL | Helper | Controller | |---|---|---|---| | `namespace :admin` | `/admin/stories` | `admin_stories_path` | `Admin::StoriesController` | | `scope module: :admin` | `/stories` | `stories_path` | `Admin::StoriesController` | | `scope path: "/admin"` | `/admin/stories` | `stories_path` | `StoriesController` | | `scope as: "admin"` | `/stories` | `admin_stories_path` | `StoriesController` | `scope "admin"` with a plain string is shorthand for `scope path: "admin"`. The options combine: `scope path: "/desk", module: :admin, as: :desk` gives `/desk/stories`, `desk_stories_path` and `Admin::StoriesController`. ## Typical uses of each 1. **`namespace`** for a real section of the app with its own controllers: an admin area, an editors' desk. 2. **`scope module:`** when controllers are organised into a module but the public URLs should not show it, such as `Public::StoriesController` behind `/stories`. 3. **`scope path:`** when a URL prefix is wanted without moving code, for example a marketing-driven `/news` prefix over the existing controllers. 4. **`scope as:`** to avoid a helper-name collision between two sets of routes that share controllers. ## Overriding parts of a namespace `namespace` accepts the same kind of override as `scope`: - `namespace :admin, path: "desk"` keeps `Admin::` controllers and `admin_` helpers but serves `/desk/stories`. - `namespace :admin, as: "desk"` keeps `/admin` and `Admin::` but renames helpers to `desk_stories_path`. ## Pitfalls interviewers probe - **Expecting `scope module:` to change the URL.** It does not; only the controller lookup moves. - **Expecting `scope path:` to find `Admin::StoriesController`.** It does not; the class stays `StoriesController`. - **Forgetting the directory.** A namespaced controller must be in `app/controllers/admin/` and declared as `Admin::StoriesController`, or the request fails with a missing controller, which Rails 8.1 answers with a 500. - **Helper collisions.** Two `resources :stories` lines in different scopes without distinct `as:` would want the same helper names. Rails does not raise: the second set silently gets **no** default names, so its helpers simply do not exist. `namespace`, or an explicit `as:`, avoids that by prefixing them. - **`scope` versus `namespace` for one option.** `namespace :admin` behaves like `scope path: "admin", module: "admin", as: "admin"` (plus shallow defaults); reaching for `scope` makes sense only when you want fewer than all three. ## Where the controller file goes | Block | Controller file | |---|---| | `namespace :admin` | `app/controllers/admin/stories_controller.rb` | | `scope module: :admin` | `app/controllers/admin/stories_controller.rb` | | `scope path: "/admin"` | `app/controllers/stories_controller.rb` | | `scope as: "admin"` | `app/controllers/stories_controller.rb` | Only the options that set a module move the file; URL and helper changes never do. Being able to fill in that four-row table from memory is usually what the question is testing.
- In the namespace above, what is the helper for the publish member route, and why?`publish_admin_story_path(story)`. A member route's name is the action, then the scope prefix, then the singular resource name, so the namespace's `admin` sits between `publish` and `story`. The path is `/admin/stories/:id/publish`, handled by `Admin::StoriesController#publish`.
- How would you serve the admin area at /desk without renaming the Admin:: controllers or the admin_ helpers?Use `namespace :admin, path: "desk"`. The namespace still sets `module: "admin"` and the `admin` helper prefix, while `path:` replaces only the URL segment, so `/desk/stories` reaches `Admin::StoriesController` and templates keep calling `admin_stories_path`.
- scope module: :admin moves the URLs to /admin
- scope path: "/admin" routes to Admin::StoriesController
- namespace changes the controller class but not the helper names
- scope as: "admin" changes the URL prefix
- namespace :admin needs a separate module: option to find Admin::StoriesController