0
0
Ruby on Railsframework~10 mins

Member and collection routes in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Member and collection routes
Define resource routes
Add collection routes?
NoSkip collection routes
|Yes
Add routes without ID
Add member routes?
NoSkip member routes
|Yes
Add routes with ID
Routes ready for use
Rails first defines resource routes, then adds collection routes without IDs, then member routes with IDs, making routes ready.
Execution Sample
Ruby on Rails
resources :books do
  collection do
    get 'search'
  end
  member do
    post 'toggle'
  end
end
Defines routes for books with a collection route 'search' and a member route 'toggle'.
Execution Table
StepActionRoute AddedPath ExampleNotes
1Define standard RESTful routes for booksindex, show, new, create, edit, update, destroy/books, /books/:id, /books/new, etc.Standard resource routes with :id for member actions
2Add collection route 'search'search/books/searchNo :id in path, applies to collection
3Add member route 'toggle'toggle/books/:id/toggleIncludes :id in path, applies to single book
4Routes ready for useAll routes combinedVarious paths as aboveRouting table complete
💡 All member and collection routes defined for resource 'books'
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
routesemptyindex, show, new, create, edit, update, destroyAdded 'search' collection routeAdded 'toggle' member routeComplete routes for books
Key Moments - 2 Insights
Why does the collection route not include an :id in its path?
Because collection routes apply to the whole set, not a single item, so the path is like /books/search without an :id, as shown in execution_table row 2.
Why does the member route include an :id in its path?
Member routes act on a single resource item, so the path includes :id, like /books/:id/toggle, as seen in execution_table row 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the path for the collection route 'search'?
A/books/search/:id
B/books/:id/search
C/books/search
D/books/:id/toggle
💡 Hint
Check execution_table row 2 under 'Path Example'
At which step is the member route 'toggle' added?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
See execution_table row 3 under 'Step' and 'Route Added'
If you remove the collection block, which route disappears?
Asearch
Btoggle
Cindex
Dshow
💡 Hint
Refer to variable_tracker and execution_table rows 2 and 3
Concept Snapshot
resources :books do
  collection do
    get 'search'  # No :id, applies to all books
  end
  member do
    post 'toggle' # Includes :id, applies to one book
  end
end

- Collection routes: no :id, act on collection
- Member routes: with :id, act on single resource
- Both add custom routes inside resources block
Full Transcript
In Rails routing, member and collection routes extend resource routes. Collection routes apply to the entire set and have paths without an :id, like /books/search. Member routes apply to individual items and include :id in the path, like /books/:id/toggle. The flow starts by defining standard resource routes, then adds collection routes without :id, and finally member routes with :id. This creates a complete routing table for the resource. Understanding the difference helps you add custom actions correctly.