In config/routes.rb, how do shallow_path and shallow_prefix change the shallow member routes of episodes, and why does a namespace set them?
answer
- options on scope, not resources
- shallow_path prefixes the URL
- shallow_prefix prefixes the helper
- collection routes keep normal scope
- namespace sets both to its name
basics
~20 sshallow_path adds a path segment in front of shallow member URLs and shallow_prefix adds a prefix to their helper names; collection routes ignore both. A namespace sets both to its own name, so its shallow routes stay inside it.
solid answer
~30 sShallow member routes are drawn outside their parent, and by default outside any enclosing path too. `shallow_path` and `shallow_prefix`, set on a `scope`, say where those routes go instead. Under `scope shallow_path: "studio"`, a shallow `resources :episodes` nested in shows serves `show` at `/studio/episodes/:id`; under `scope shallow_prefix: "studio"` the helper becomes `studio_episode_path` and the edit helper `edit_studio_episode_path`. The nested collection routes (`/shows/:show_id/episodes`) are untouched. `namespace :studio` sets both from its name, and a scope with `path:` or `as:` sets the matching one, so shallow routes declared inside them keep the `/studio` URL and `studio_` helpers.
code
ruby · 12 lines# config/routes.rb
Rails.application.routes.draw do
namespace :studio do
resources :shows, shallow: true do
resources :episodes
end
end
end
# GET /studio/shows/3/episodes -> studio/episodes#index studio_show_episodes_path(show)
# GET /studio/episodes/42 -> studio/episodes#show studio_episode_path(episode)
# GET /studio/episodes/42/edit -> studio/episodes#edit edit_studio_episode_path(episode)go deeper
Know that shallow_path changes where shallow member URLs live and shallow_prefix changes their helper names.
Explain that both apply only to the shallow member half, and that namespace, scope path: and scope as: set them for you.
Use them to keep an admin or studio section's shallow routes inside that section, and verify the result with bin/rails routes.
Prefer the defaults a namespace provides over hand-set shallow options, which make the route table harder for the next reader to predict.
## The problem the options solve With **shallow nesting**, member routes (`show`, `edit`, `update`, `destroy`) are lifted out of their parent: an episode nested under shows is served at `/episodes/:id` with `episode_path(episode)`, while `index`, `new` and `create` stay under `/shows/:show_id/episodes`. "Lifted out" raises a question when the whole block sits inside a scope. Where should `/episodes/:id` go if the shows live under `/studio`? Rails answers with two scope options that apply **only** to those shallow member routes: - **`shallow_path`** is a path segment put in front of shallow member URLs. - **`shallow_prefix`** is a name put in front of shallow member helper names. ## shallow_path ```ruby scope shallow_path: "studio" do resources :shows do resources :episodes, shallow: true end end ``` | Action | Path | Helper | |---|---|---| | `index` | `/shows/:show_id/episodes` | `show_episodes_path(show)` | | `new` | `/shows/:show_id/episodes/new` | `new_show_episode_path(show)` | | `show` | `/studio/episodes/:id` | `episode_path(episode)` | | `edit` | `/studio/episodes/:id/edit` | `edit_episode_path(episode)` | Only the URL of the shallow member routes moved; their helpers keep their usual names, and the nested collection routes are unaffected. ## shallow_prefix ```ruby scope shallow_prefix: "studio" do resources :shows do resources :episodes, shallow: true end end ``` Now the shallow member URLs stay `/episodes/:id`, but their helpers are renamed: `studio_episode_path(episode)` for `show`, `update` and `destroy`, and `edit_studio_episode_path(episode)` for `edit`. The `edit_` part stays at the front, as it does for every Rails helper. Collection helpers such as `show_episodes_path(show)` are unchanged. ## namespace sets both A `namespace` block, the usual way to build an admin or back-office area, sets **both** options from its name: ```ruby namespace :studio do resources :shows, shallow: true do resources :episodes end end ``` That gives: 1. collection routes at `/studio/shows/:show_id/episodes` with `studio_show_episodes_path(show)`; 2. shallow member routes at `/studio/episodes/:id` with `studio_episode_path(episode)`; 3. controllers in the `Studio` module, here `Studio::EpisodesController`, for both groups. Without that default, the shallow member routes of a namespaced resource would escape to `/episodes/:id` and lose the `studio_` helper prefix while still being dispatched to `Studio::EpisodesController`, a confusing mix. A `scope` with `path:` sets `shallow_path` the same way, and a `scope` with `as:` sets `shallow_prefix`, unless you pass the shallow options yourself. A scope with only `module:` sets neither, so its shallow member routes do sit at `/episodes/:id`. ## Setting both on one scope The two options are independent and can be combined on one `scope`: ```ruby scope shallow_path: "studio", shallow_prefix: "studio" do resources :shows do resources :episodes, shallow: true end end ``` That moves the shallow member routes to `/studio/episodes/:id` **and** renames them `studio_episode_path`, while the collection routes stay at `/shows/:show_id/episodes` with `show_episodes_path`. It is the shallow half of what a namespace does, without the namespace's effect on the collection routes or the controller module. ## Mistakes interviewers listen for | Belief | What Rails actually does | |---|---| | `shallow_path` moves `index` and `create` too | It prefixes only the shallow member URLs | | `shallow_prefix` changes the URL | It changes helper names only | | A namespace's shallow routes escape to `/episodes/:id` | `namespace` sets both options from its name | | `scope module:` sets `shallow_path` | Only `path:` and `as:` set the shallow options | ## When you would set them by hand - To keep shallow member URLs **inside a section** whose scope did not set them, for example one built with `module:` alone. - To give shallow routes a **different prefix** from the collection routes, which is rare and worth a comment. - To **remove** the prefix a namespace would add, by passing `shallow_path: nil` or `shallow_prefix: nil` to `namespace`, when member URLs should be shared with the public site. ## Reading the result Because these options change only half of a resource's routes, reasoning about them from memory is error-prone. `bin/rails routes -c episodes` lists every path and helper name, and is the quickest way to confirm where the shallow half went.
- With scope module: "studio" around a shallow nested episodes resource, where do the member routes go?To `/episodes/:id` with `episode_path(episode)`, dispatched to `Studio::EpisodesController`. `module:` only changes the controller namespace and sets neither `shallow_path` nor `shallow_prefix`, so the shallow member routes are drawn at the top level of the URL space while their controller lives in the `Studio` module.
- shallow_path also moves the nested index and create routes
- shallow_prefix changes the URL rather than the helper names
- A namespace's shallow member routes escape to /episodes/:id by default
- shallow_prefix: "studio" turns the edit helper into studio_edit_episode_path
- scope module: sets shallow_path to the module name