Bird
Raised Fist0
Ruby on Railsframework~10 mins

Member and collection routes in Ruby on Rails - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a member route named 'preview' inside the 'resources :articles' block.

Ruby on Rails
resources :articles do
  member do
    get '[1]'
  end
end
Drag options to blanks, or click blank then click option'
Ashow
Bindex
Cpreview
Dedit
Attempts:
3 left
💡 Hint
Common Mistakes
Using collection route names inside member block.
Using standard RESTful actions like 'index' inside member block.
2fill in blank
medium

Complete the code to define a collection route named 'search' inside the 'resources :books' block.

Ruby on Rails
resources :books do
  collection do
    get '[1]'
  end
end
Drag options to blanks, or click blank then click option'
Asearch
Bedit
Cshow
Dnew
Attempts:
3 left
💡 Hint
Common Mistakes
Using member route names inside collection block.
Using singular resource actions like 'show' inside collection block.
3fill in blank
hard

Fix the error in the code by choosing the correct keyword to define a member route named 'toggle'.

Ruby on Rails
resources :posts do
  [1] do
    post 'toggle'
  end
end
Drag options to blanks, or click blank then click option'
Acollection
Bmember
Cresource
Dresources
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'collection' instead of 'member' for single resource actions.
Using 'resource' or 'resources' keywords incorrectly inside the block.
4fill in blank
hard

Fill both blanks to define a collection route 'archive' and a member route 'publish' inside 'resources :events'.

Ruby on Rails
resources :events do
  [1] do
    get 'archive'
  end
  [2] do
    post 'publish'
  end
end
Drag options to blanks, or click blank then click option'
Acollection
Bmember
Cresource
Dresources
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping member and collection keywords.
Using 'resource' or 'resources' inside the block incorrectly.
5fill in blank
hard

Fill all three blanks to define a member route 'activate', a collection route 'stats', and a member route 'deactivate' inside 'resources :users'.

Ruby on Rails
resources :users do
  [1] do
    post 'activate'
    post 'deactivate'
  end
  [2] do
    get 'stats'
  end
  [3] do
    get 'profile'
  end
end
Drag options to blanks, or click blank then click option'
Amember
Bcollection
Dresource
Attempts:
3 left
💡 Hint
Common Mistakes
Putting member routes inside collection block.
Using 'resource' keyword incorrectly.

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