Bird
Raised Fist0
Ruby on Railsframework~5 mins

Member and collection routes in Ruby on Rails - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is a member route in Rails routing?
A member route is a custom route that acts on a single resource identified by its ID. It adds a route that requires the resource's ID in the URL.
Click to reveal answer
beginner
What is a collection route in Rails routing?
A collection route is a custom route that acts on the entire collection of resources, without needing an ID in the URL.
Click to reveal answer
intermediate
How do you define a member route in Rails routes.rb file?
Inside a resource block, use member do and then define the HTTP verb and action, for example:
resources :books do
member do
get 'preview'
end
end
Click to reveal answer
intermediate
How do you define a collection route in Rails routes.rb file?
Inside a resource block, use collection do and then define the HTTP verb and action, for example:
resources :books do
collection do
get 'search'
end
end
Click to reveal answer
beginner
What is the difference in URL structure between member and collection routes?
Member routes include the resource ID in the URL, like /books/:id/preview. Collection routes do not include an ID, like /books/search.
Click to reveal answer
Which route type requires the resource ID in the URL?
ANamespace route
BCollection route
CMember route
DRoot route
How do you define a collection route inside a resource block?
AUsing <code>collection do</code>
BUsing <code>resource do</code>
CUsing <code>member do</code>
DUsing <code>namespace do</code>
What URL would a member route named 'preview' generate for a book with ID 5?
A/books/5/preview
B/books/preview
C/preview/books/5
D/books/5
Which HTTP verb is commonly used for a collection route that lists filtered results?
ADELETE
BGET
CPOST
DPUT
If you want to add a custom action that applies to all books, which route type should you use?
ARoot route
BMember route
CSingular resource route
DCollection route
Explain the difference between member and collection routes in Rails with examples.
Think about whether the route needs an ID or not.
You got /4 concepts.
    How do you add a custom action to a resource in Rails routes.rb using member and collection blocks?
    Remember the block names and URL patterns.
    You got /4 concepts.

      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