Bird
Raised Fist0
Ruby on Railsframework~20 mins

Member and collection routes in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Master of Member and Collection Routes
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Output of member route URL helper
Given the following routes in a Rails application:
resources :books do
  member do
    get 'preview'
  end
end

What is the output of preview_book_path(5)?
A/books/5/preview
B/books/preview/5
C/preview/books/5
D/books/5/preview_book
Attempts:
2 left
💡 Hint
Member routes add actions for a single resource identified by ID.
❓ query_result
intermediate
2:00remaining
Output of collection route URL helper
Given the following routes in a Rails application:
resources :orders do
  collection do
    get 'recent'
  end
end

What is the output of recent_orders_path?
A/recent/orders
B/orders/:id/recent
C/orders/recent_orders
D/orders/recent
Attempts:
2 left
💡 Hint
Collection routes add actions that apply to the whole collection, no ID needed.
📝 Syntax
advanced
2:00remaining
Identify the invalid member route syntax
Which option contains invalid syntax for defining a member route inside resources :products?
A
member do
  delete 'remove'
end
Bmember get 'details'
C
member do
  post 'purchase'
end
D
member do
  get 'details'
end
Attempts:
2 left
💡 Hint
Member routes require a block with do...end or braces.
🧠 Conceptual
advanced
2:00remaining
Difference between member and collection routes
Which statement correctly describes the difference between member and collection routes in Rails?
ABoth member and collection routes always include the resource ID in the URL.
BMember routes apply to the entire collection; collection routes apply to a single resource with an ID.
CMember routes apply to a single resource and include the resource ID in the URL; collection routes apply to the entire collection without an ID.
DMember routes are only for POST requests; collection routes are only for GET requests.
Attempts:
2 left
💡 Hint
Think about whether the route URL includes an ID or not.
🔧 Debug
expert
3:00remaining
Why does this member route cause a routing error?
Consider this routes definition:
resources :users do
  member do
    get 'profile/:section'
  end
end

Why will calling profile_user_path(3, section: 'settings') cause a routing error?
AMember routes cannot have dynamic segments like ':section' inside the block.
BThe dynamic segment ':section' must be declared outside the member block.
CThe route helper expects two arguments, but only one is given.
DThe route helper method name is incorrect; it should be 'profile_section_user_path'.
Attempts:
2 left
💡 Hint
Member routes define actions for a single resource; dynamic segments inside member blocks are not supported this way.

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

  1. Step 1: Understand member route purpose

    A member route targets one specific item, so it needs the item's ID in the URL.
  2. Step 2: Compare with collection route

    Collection routes act on all items and do not include an ID.
  3. Final Answer:

    It acts on a single resource and includes the resource ID in the URL. -> Option A
  4. 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

  1. Step 1: Identify member route syntax

    Member routes are defined inside a member do ... end block with the HTTP verb and action name.
  2. 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.
  3. Final Answer:

    member do get 'preview' end -> Option B
  4. 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

  1. Step 1: Understand member route URL pattern

    Member routes add the action after the resource ID, so the URL is /books/:id/action.
  2. Step 2: Apply to details_book_path(5)

    With ID 5 and action 'details', the URL is /books/5/details.
  3. Final Answer:

    /books/5/details -> Option C
  4. 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

  1. 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.
  2. Step 2: Analyze the error cause

    Calling profile_user_path(3) passes an ID, but collection routes ignore IDs, causing an error.
  3. Final Answer:

    Collection routes do not accept an ID parameter in the path helper. -> Option D
  4. 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

  1. Step 1: Identify the route scope

    Listing all orders placed today acts on the whole collection, so it needs a collection route.
  2. Step 2: Choose correct route definition

    Defining get 'today' inside a collection block adds a route without requiring an ID.
  3. Final Answer:

    Use a collection route named today with get 'today' inside a collection block. -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard