Practice
1. Which statement best describes a
member route in Rails?easy
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]
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
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]
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:
What URL will be generated by
resources :books do
member do
get 'details'
end
collection do
get 'search'
end
endWhat URL will be generated by
details_book_path(5)?medium
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]
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
But calling
resources :users:collection do get 'profile' end
But calling
profile_user_path(3) gives an error. Why?medium
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]
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
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]
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
