Discover how to make your app's special actions neat and easy with member and collection routes!
Why Member and collection routes in Ruby on Rails? - Purpose & Use Cases
Imagine you have a website with many items, like books. You want to add special actions: one that works on a single book (like marking it as favorite) and another that works on all books (like showing all favorites). Doing this by hand means writing many separate URLs and code for each case.
Manually creating URLs for each item and for the whole collection is slow and confusing. You might mix up URLs or forget to handle some cases. It's easy to make mistakes and hard to keep track of all routes as your app grows.
Member and collection routes let you cleanly define actions for one item or for the whole group inside your routes file. Rails automatically creates the right URLs and helpers, so you write less code and avoid errors.
get '/books/:id/favorite' => 'books#favorite' get '/books/favorites' => 'books#favorites'
resources :books do
member do
get 'favorite'
end
collection do
get 'favorites'
end
endYou can easily add custom actions for single items or entire collections with clear, maintainable routes and helpers.
On a photo gallery site, you might want to add a 'like' button for each photo (member route) and a page showing all liked photos (collection route). Member and collection routes make this simple and organized.
Manual URL handling is error-prone and hard to maintain.
Member routes handle actions on single items.
Collection routes handle actions on groups of items.
Practice
member route in Rails?Solution
Step 1: Understand member route purpose
A member route targets one specific item, so it needs the item's ID in the URL.Step 2: Compare with collection route
Collection routes act on all items and do not include an ID.Final Answer:
It acts on a single resource and includes the resource ID in the URL. -> Option AQuick Check:
Member route = single item with ID [OK]
- Confusing member with collection routes
- Thinking member routes act on all items
- Assuming member routes create resources
preview inside a resources :posts block?Solution
Step 1: Identify member route syntax
Member routes are defined inside amember do ... endblock with the HTTP verb and action name.Step 2: Check options
member do get 'preview' end correctly usesmember do get 'preview' end. collection do get 'preview' end uses collection incorrectly. get 'preview', on: :collection uses collection syntax. get 'preview', on: :resource uses invalid:resource.Final Answer:
member do get 'preview' end -> Option BQuick Check:
Member route syntax = member do ... end [OK]
- Using collection instead of member for single item routes
- Wrong option for 'on' parameter
- Syntax errors in block structure
resources :books do
member do
get 'details'
end
collection do
get 'search'
end
endWhat URL will be generated by
details_book_path(5)?Solution
Step 1: Understand member route URL pattern
Member routes add the action after the resource ID, so the URL is /books/:id/action.Step 2: Apply to details_book_path(5)
With ID 5 and action 'details', the URL is /books/5/details.Final Answer:
/books/5/details -> Option CQuick Check:
Member route URL = /resource/:id/action [OK]
- Placing action before ID in URL
- Confusing collection route URL pattern
- Using query parameters instead of path segments
resources :users:collection do get 'profile' end
But calling
profile_user_path(3) gives an error. Why?Solution
Step 1: Understand collection route behavior
Collection routes act on the whole group and do not include an ID in the URL or path helper.Step 2: Analyze the error cause
Callingprofile_user_path(3)passes an ID, but collection routes ignore IDs, causing an error.Final Answer:
Collection routes do not accept an ID parameter in the path helper. -> Option DQuick Check:
Collection route helpers exclude ID [OK]
- Passing ID to collection route helpers
- Misplacing routes inside member vs collection
- Wrong HTTP verb assumption
resources :orders that lists all orders placed today. Which is the best way to define this route?Solution
Step 1: Identify the route scope
Listing all orders placed today acts on the whole collection, so it needs a collection route.Step 2: Choose correct route definition
Definingget 'today'inside a collection block adds a route without requiring an ID.Final Answer:
Use a collection route named today with get 'today' inside a collection block. -> Option AQuick Check:
Collection route for group actions = Use a collection route namedtodaywithget 'today'inside a collection block. [OK]
- Using member route for group action
- Creating unnecessary new resource
- Passing ID to collection route
Ready to prove it?
Pass the test and this pattern moves unknown
