📖 Scenario: You are building a simple blog application in Rails. You want to add special routes to manage posts. Some routes will act on individual posts (member routes), and some will act on the whole collection of posts (collection routes).
🎯 Goal: Create member and collection routes inside the resources :posts block in config/routes.rb. Add a member route called preview that shows a preview of a single post. Add a collection route called search that allows searching all posts.
📋 What You'll Learn
Create a resources :posts block in config/routes.rb
Inside the block, add a member route get 'preview'
Inside the block, add a collection route get 'search'
💡 Why This Matters
🌍 Real World
Member and collection routes help you add custom actions in Rails apps that work on single items or entire collections, like previewing a post or searching all posts.
💼 Career
Understanding how to customize routes with member and collection blocks is essential for Rails developers to build flexible and maintainable web applications.
Progress0 / 4 steps
1
Create the basic posts resource route
In config/routes.rb, create a resources :posts block to define standard RESTful routes for posts.
Ruby on Rails
Hint
Use resources :posts inside the Rails.application.routes.draw do ... end block.
2
Add a member route for preview
Inside the resources :posts block, add a member route get 'preview' to allow previewing a single post.
Ruby on Rails
Hint
Use member do block inside resources :posts and add get 'preview' inside it.
3
Add a collection route for search
Inside the resources :posts block, add a collection route get 'search' to allow searching all posts.
Ruby on Rails
Hint
Use collection do block inside resources :posts and add get 'search' inside it.
4
Complete the routes with both member and collection routes
Ensure the resources :posts block contains both the member route get 'preview' and the collection route get 'search' inside their respective blocks.
Ruby on Rails
Hint
Check that both member do and collection do blocks are inside resources :posts with their respective get routes.
Practice
(1/5)
1. Which statement best describes a member route in Rails?
easy
A. It acts on a single resource and includes the resource ID in the URL.
B. It acts on the entire collection without an ID in the URL.
C. It is used only for creating new resources.
D. It automatically generates all CRUD routes.
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 A
Quick Check:
Member route = single item with ID [OK]
Hint: Member routes include ID; collection routes do not. [OK]
Common Mistakes:
Confusing member with collection routes
Thinking member routes act on all items
Assuming member routes create resources
2. Which of the following is the correct syntax to add a member route named preview inside a resources :posts block?
easy
A. get 'preview', on: :collection
B. member do
get 'preview'
end
C. collection do
get 'preview'
end
D. get 'preview', on: :resource
Solution
Step 1: Identify member route syntax
Member routes are defined inside a member do ... end block with the HTTP verb and action name.
Step 2: Check options
member do
get 'preview'
end correctly uses member 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 B
Quick Check:
Member route syntax = member do ... end [OK]
Hint: Member routes use 'member do' block, not 'collection do'. [OK]
Common Mistakes:
Using collection instead of member for single item routes
Wrong option for 'on' parameter
Syntax errors in block structure
3. Given the routes defined as:
resources :books do
member do
get 'details'
end
collection do
get 'search'
end
end
What URL will be generated by details_book_path(5)?
medium
A. /books/details/5
B. /books/search/5
C. /books/5/details
D. /books/details?id=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 C
Quick Check:
Member route URL = /resource/:id/action [OK]
Hint: Member route URLs put action after ID in path. [OK]
Common Mistakes:
Placing action before ID in URL
Confusing collection route URL pattern
Using query parameters instead of path segments
4. You wrote this route inside resources :users:
collection do
get 'profile'
end
But calling profile_user_path(3) gives an error. Why?
medium
A. The route should be inside a member block, not collection.
B. The route name must be plural, like profiles.
C. The HTTP verb should be POST, not GET.
D. Collection routes do not accept an ID parameter in the path helper.
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
Calling profile_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 D
Quick Check:
Collection route helpers exclude ID [OK]
Hint: Collection routes ignore IDs; member routes require them. [OK]
Common Mistakes:
Passing ID to collection route helpers
Misplacing routes inside member vs collection
Wrong HTTP verb assumption
5. You want to add a route to resources :orders that lists all orders placed today. Which is the best way to define this route?
hard
A. Use a collection route named today with get 'today' inside a collection block.
B. Use a member route named today with get 'today' inside a member block.
C. Add a new resource today_orders with standard CRUD routes.
D. Use a collection route named today but pass an order ID parameter.
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
Defining get '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 A
Quick Check:
Collection route for group actions = Use a collection route named today with get 'today' inside a collection block. [OK]
Hint: Group actions use collection routes without IDs. [OK]
Common Mistakes:
Using member route for group action
Creating unnecessary new resource
Passing ID to collection route
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.