skip to content

Member, Collection & Namespaces

Member and collection blocks extend a resource, get, post and match map one-off URLs, and namespace, scope and module: group them. Interviewers test which option changes the path, helper or class.

part ofRuby on Railsread it →
on this pageshow

explore

questions

5

In config/routes.rb, how do member and collection routes added to resources :stories differ in path, params and helper name?

level: middleimportance: must knowfreq 65%

answer

  1. one record versus the whole set
  2. member path carries :id
  3. helper puts the action first
  4. on: is the one-line form
  5. a bare get nests instead

basics

~10 s

A 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 s

Inside `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
ruby
# 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_path

go deeper

for a junior

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.

for a middle

Derive the path, params and helper name of any member or collection route, use the on: forms, and explain the bare-route nesting trap.

for a senior

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.

for a principal

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
open as a page

In config/routes.rb, how do namespace :admin, scope module: :admin, scope path: and scope as: differ in URL, helper name and controller class?

level: middleimportance: must knowfreq 60%

basics

~10 s

namespace :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.

open as a page

In config/routes.rb, how do root, get with to: "stories#preview" and match with via: map one-off URLs, and how are those routes named?

level: juniorimportance: should knowfreq 55%

basics

~10 s

root "stories#index" maps GET / and defines root_path; get "stories/:id/preview", to: "stories#preview", as: :preview_story maps one verb and path to an action; match does the same for several verbs and raises ArgumentError without via:.

open as a page

In config/routes.rb, how do optional segments, the (.:format) suffix and defaults: shape a route like get "archive/:year(/:month)"?

level: middleimportance: should knowfreq 35%

basics

~10 s

Parentheses make a segment optional, so archive/:year(/:month) matches /archive/2026 and /archive/2026/10; Rails appends an optional (.:format) unless format: false; defaults: supplies params a URL leaves out, such as format: "rss".

open as a page

In a large Rails app's config/routes.rb, how do draw and mount help organise routing, and what does each one actually hand off?

level: seniorimportance: nice to knowfreq 18%

basics

~10 s

draw :admin evaluates config/routes/admin.rb in place, inside the current scope, so a big routes file can be split; mount hands every request under a path prefix, for any verb, to another Rack application.

open as a page