In config/routes.rb, which routes, helper names and params does resources :episodes nested inside resources :shows produce?
answer
- parent segment before every path
- singular parent name plus _id
- helper names gain a prefix
- parent first, then child
- controller stays EpisodesController
basics
~10 sNesting puts /shows/:show_id in front of all seven episode routes, prefixes the helpers (show_episodes_path(show), show_episode_path(show, episode)), passes params[:show_id] next to params[:id], and still dispatches to a plain EpisodesController.
solid answer
~30 sInside `resources :shows do ... end`, `resources :episodes` draws its usual seven actions under a parent segment: `GET /shows/:show_id/episodes` for `index`, `POST` there for `create`, `/shows/:show_id/episodes/new`, and member routes on `/shows/:show_id/episodes/:id`. The parent key is the parent's singular name plus `_id`, so the action sees `params[:show_id]` beside `params[:id]`. Helper names gain the parent's singular as a prefix: `show_episodes_path(show)`, `new_show_episode_path(show)`, `show_episode_path(show, episode)`, `edit_show_episode_path(show, episode)`, with the parent passed first. Nesting changes paths and names only: the controller is still `EpisodesController`, not a namespaced class, and the show routes themselves are drawn as before.
code
ruby · 10 lines# config/routes.rb
Rails.application.routes.draw do
resources :shows do
resources :episodes
end
end
# GET /shows/3/episodes/42 -> EpisodesController#show
# params[:show_id] == "3", params[:id] == "42"
# show_episode_path(@show, @episode) # => "/shows/3/episodes/42"go deeper
Recall the path shape /shows/:show_id/episodes/:id, the prefixed helper names with the parent passed first, and that the controller is still EpisodesController.
Explain where each name comes from: the parent's singular plus _id for the segment, the parent's singular as a helper prefix, and why the router does not check ownership.
Point out the ownership check the controller must do and the cost every nested helper call adds to templates, which leads to keeping nesting shallow.
Decide which resources are truly owned by a parent and deserve nested URLs, and keep the rest top-level so the route table stays readable.
## Why nest at all A podcast-hosting app has **shows**, and each show has **episodes**. An episode never exists outside its show, and most screens start from a show: "all episodes of this show", "add an episode to this show". Rails' routing DSL lets `config/routes.rb` express that ownership by **nesting** one resource inside another's block: ```ruby Rails.application.routes.draw do resources :shows do resources :episodes end end ``` The outer line still draws the seven show routes (`/shows`, `/shows/:id` and so on). The inner line draws seven more, each one scoped under a single show. ## The routes | Verb | Path | Action | Helper | |---|---|---|---| | GET | `/shows/:show_id/episodes` | `episodes#index` | `show_episodes_path(show)` | | GET | `/shows/:show_id/episodes/new` | `episodes#new` | `new_show_episode_path(show)` | | POST | `/shows/:show_id/episodes` | `episodes#create` | `show_episodes_path(show)` | | GET | `/shows/:show_id/episodes/:id` | `episodes#show` | `show_episode_path(show, episode)` | | GET | `/shows/:show_id/episodes/:id/edit` | `episodes#edit` | `edit_show_episode_path(show, episode)` | | PATCH / PUT | `/shows/:show_id/episodes/:id` | `episodes#update` | `show_episode_path(show, episode)` | | DELETE | `/shows/:show_id/episodes/:id` | `episodes#destroy` | `show_episode_path(show, episode)` | ## Three naming rules 1. **The parent segment** is the parent's singular name plus `_id`: `:show_id`. The child keeps its own `:id`. (If the parent resource renamed its parameter with `param:`, the parent key follows that name, for example `:show_slug`.) 2. **Helper names** are the child's usual names with the parent's singular in front: `show_episodes`, `new_show_episode`, `edit_show_episode`, `show_episode`. The `new_` and `edit_` prefixes stay at the very front. 3. **Helper arguments** follow the path from left to right: the show first, then the episode. A helper with a missing required segment raises `ActionController::UrlGenerationError` naming the missing keys. ## What does not change - **The controller.** Nesting is about URLs, not about classes: the routes dispatch to `EpisodesController`, exactly as a top-level `resources :episodes` would. Moving the controller into a `Shows::` namespace is a separate decision, made with `module:` or a namespace block. - **The child's actions.** The seven action names are the same, and `only:` and `except:` work on the nested line as on any other. - **The parent's routes.** `/shows/:id` and its helpers are drawn by the outer line as usual. - **Top-level episode URLs.** Nesting draws **no** `/episodes/:id`. If the app also needs one, it must be declared separately or produced by shallow nesting. ## What the controller receives For `GET /shows/3/episodes/42` the path parameters are `show_id: "3"` and `id: "42"`, both strings, alongside `controller` and `action`. The router only matches the pattern: it does not check that episode 42 belongs to show 3. That check is the controller's job, usually by finding the show first and then looking the episode up through it, so a URL with a mismatched pair finds nothing instead of leaking another show's episode. ## Trimming each level separately `only:` and `except:` belong to the line they are written on. A public catalogue that lists shows and plays episodes might write: ```ruby resources :shows, only: [:index, :show] do resources :episodes, only: [:index, :show] end ``` - The parent's list does **not** flow into the child: without its own `only:`, the nested line would draw all seven episode routes. - The child's routes do **not** depend on the parent's actions: `/shows/:show_id/episodes` exists even if the parent drew no `show` route, because the nested segment is built from the parent's name, not from its routes. - Each level is checked separately, so a typo in either list raises `ArgumentError` naming that line. ## Common slips - **Expecting `params[:episode_id]`** in `EpisodesController`. The child's own key is `:id`; `:episode_id` only appears in routes nested one level deeper under episodes. - **Calling `episode_path(episode)`** after nesting. Plain nesting defines no such helper, so the call raises `NameError` unless something else draws it. - **Passing arguments in the wrong order.** `show_episode_path(episode, show)` builds a path, but with the ids swapped, and the page then finds the wrong records or none. - **Assuming a namespaced controller.** Requests go to `EpisodesController` in `app/controllers/episodes_controller.rb`, not to `app/controllers/shows/episodes_controller.rb`. Interviewers ask this to check that a candidate can read a nested block and say, without running `bin/rails routes`, which URL, helper and params each action gets.
- Does nesting guarantee that episode 42 belongs to show 3?No. The router only matches the URL pattern and hands both ids to `EpisodesController`. If the action looks the episode up by `params[:id]` alone, `/shows/3/episodes/42` would serve an episode from any show. The ownership check belongs in the controller, typically by loading the show and finding the episode through it.
- How would you also keep episodes out of the app's top-level URL space while still listing them per show?Plain nesting already does that: it draws only `/shows/:show_id/episodes...` routes and no top-level `/episodes` path. Adding `only:` to the nested line trims actions further, for example `resources :episodes, only: [:index, :show]` for a read-only public catalogue.
- Nested episodes routes dispatch to a Shows::EpisodesController
- The nested action reads the episode id from params[:episode_id]
- The router rejects /shows/3/episodes/42 when episode 42 belongs to another show
- episode_path(episode) still works after nesting episodes under shows
- Nesting removes the shows routes the outer line would have drawn