In config/routes.rb, how do member and collection routes added to resources :stories differ in path, params and helper name?
answer
- one record versus the whole set
- member path carries :id
- helper puts the action first
- on: is the one-line form
- a bare get nests instead
basics
~10 sA member route acts on one story, so post :publish inside member gives POST /stories/:id/publish with params[:id] and publish_story_path(story); a collection route acts on the set, so get :drafts gives GET /stories/drafts and drafts_stories_path.
solid answer
~30 sInside `resources :stories do ... end`, a `member` block adds routes under `/stories/:id`: `post :publish` draws `POST /stories/:id/publish` to `stories#publish`, the action reads `params[:id]`, and the helper is `publish_story_path(story)`. A `collection` block adds routes under `/stories` with no id: `get :drafts` draws `GET /stories/drafts` and `drafts_stories_path`. The one-line forms are `post :publish, on: :member` and `get :drafts, on: :collection`. Helper names put the action first, then the singular (member) or plural (collection) name. Leaving out `member`/`on:` is a trap: a bare `post :publish` in the block is drawn as a nested route, `/stories/:story_id/publish`, with `params[:story_id]` and `story_publish_path`.
code
ruby · 10 lines# config/routes.rb
Rails.application.routes.draw do
resources :stories do
post :publish, on: :member
get :drafts, on: :collection
end
end
# POST /stories/:id/publish -> stories#publish publish_story_path(story)
# GET /stories/drafts -> stories#drafts drafts_stories_pathgo deeper
Know that member routes act on one record and include :id, collection routes act on the set, and both are declared inside the resources block.
Derive the path, params and helper name of any member or collection route, use the on: forms, and explain the bare-route nesting trap.
Spot when custom actions are piling up on one controller and a separate resource would read better, and keep custom routes in the block so ordering stays correct.
Set a team rule for when a custom member or collection route is acceptable and when a verb deserves its own resource.
## Beyond the seven actions A newsroom CMS draws `resources :stories` in `config/routes.rb` and gets the seven standard actions. Two needs do not fit them: an editor **publishes** one story, and the desk wants a list of **drafts**. Rails' routing DSL lets you add such routes inside the resource's block, and it distinguishes them by what they act on: - a **member** route acts on **one existing record**, so its path carries the record's `:id`; - a **collection** route acts on **the set**, so its path has no id. ```ruby resources :stories do member do post :publish end collection do get :drafts end end ``` ## What each one draws | Route | Verb and path | Action | Params | Helper | |---|---|---|---|---| | member `post :publish` | `POST /stories/:id/publish` | `stories#publish` | `params[:id]` | `publish_story_path(story)` | | collection `get :drafts` | `GET /stories/drafts` | `stories#drafts` | none | `drafts_stories_path` | The helper naming rule is **action first**, then the resource name: the singular for a member route (`publish_story`), the plural for a collection route (`drafts_stories`). The verb method (`get`, `post`, `patch`, `delete`) sets the HTTP verb the route answers, so a state change such as publishing belongs on `post` or `patch`, never `get`. ## One-line forms When there is only one route, the block is optional: ```ruby resources :stories do post :publish, on: :member get :drafts, on: :collection end ``` `on:` accepts `:member`, `:collection` and `:new`; anything else raises `ArgumentError`. `on: :new` places a route under the new-record path, for example `get :preview, on: :new` draws `GET /stories/new/preview` with `preview_new_story_path`, useful for previewing an unsaved form. ## The bare-route trap Inside a `resources` block, a route **without** `member`, `collection` or `on:` is not added to the resource. Rails treats it as **nested** under the resource, just as a nested `resources` line would be: - `post :publish` alone draws `POST /stories/:story_id/publish`; - the action receives `params[:story_id]`, not `params[:id]`; - the helper is `story_publish_path(story)`. Code written for a member route, such as a `before_action` that loads `params[:id]`, then finds nothing. Saying `member`, `collection` or `on:` explicitly avoids the surprise. Inside a **singular** `resource` block, by contrast, a bare route is treated as a member route, because a singular resource has no ids to nest under. ## Ordering Routes in the block are drawn **before** the resource's own seven routes, so `GET /stories/drafts` is matched before `GET /stories/:id` could swallow it as `id: "drafts"`. That is why custom routes belong inside the block rather than as separate lines after `resources :stories`: a later `get "stories/drafts"` would lose to the `show` route drawn earlier. ## When to use which 1. **Member**: a verb on one record that does not fit the seven actions: publish, archive, a full-size download, a print view. 2. **Collection**: a different view or operation on the set: drafts, search, a bulk import. 3. **Neither**: when the route grows its own rules, filters and templates, a separate small resource with standard actions often reads better than another custom action. ## Quick checks - `bin/rails routes -c stories` shows the drawn verb, path and helper for each custom route. - A member helper needs a record (`publish_story_path(@story)`); a collection helper needs none. - The verb method matters for safety as well as matching: `get :publish, on: :member` would let any link or prefetch publish a story. ## Mistakes interviewers listen for | Belief | What Rails actually does | |---|---| | The member helper is `story_publish_path` | Action first: `publish_story_path(story)` | | A collection route carries `:id` | Collection paths have no id: `/stories/drafts` | | A bare `post :publish` is a member route | In a `resources` block it is nested, with `:story_id` | | `on:` takes any name | Only `:member`, `:collection` and `:new` | | Custom routes can follow the resources line | `show` would match them first; keep them in the block |
- Why does GET /stories/drafts not reach show with params[:id] set to "drafts"?`resources` draws the routes from its block before its own seven routes, so the collection route `GET /stories/drafts` is earlier in the table than `GET /stories/:id` and matches first. A `get "stories/drafts"` written on its own line after `resources :stories` would come too late and reach `show`.
- A colleague wrote post :publish inside the resources block without on:. What breaks?Rails draws it as a nested route: `POST /stories/:story_id/publish`, helper `story_publish_path`. The action expects `params[:id]` and gets `params[:story_id]` instead, so its lookup fails, and any template calling `publish_story_path` raises `NameError`. Adding `on: :member` draws the intended `/stories/:id/publish`.
On a newsroom whiteboard, a sticky note on one story card is a member route; a note on the column header that applies to every card under it is a collection route.
- A member route's helper is story_publish_path
- Collection routes carry the record id like member routes
- A bare post :publish in the resources block becomes a member route
- on: accepts any scope name, such as :admin
- Custom collection routes can go after the resources line without any ordering issue