Bird
Raised Fist0
Ruby on Railsframework~10 mins

RESTful resource 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 RESTful routes for a resource named 'articles'.

Ruby on Rails
Rails.application.routes.draw do
  resources :[1]
end
Drag options to blanks, or click blank then click option'
Aarticle
Barticles
Cposts
Darticle_list
Attempts:
3 left
💡 Hint
Common Mistakes
Using singular form instead of plural.
Using unrelated resource names.
2fill in blank
medium

Complete the code to add only the 'index' and 'show' actions to the resource routes.

Ruby on Rails
Rails.application.routes.draw do
  resources :products, only: [:[1]]
end
Drag options to blanks, or click blank then click option'
Aindex, show
Bcreate, update
Cindex
Dnew, edit
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of an array.
Including actions not intended.
3fill in blank
hard

Fix the error in the code to correctly nest comments inside posts.

Ruby on Rails
Rails.application.routes.draw do
  resources :posts do
    resources :[1]
  end
end
Drag options to blanks, or click blank then click option'
Acomment_list
Bcomment
Cpost_comments
Dcomments
Attempts:
3 left
💡 Hint
Common Mistakes
Using singular form for nested resources.
Using incorrect resource names.
4fill in blank
hard

Fill both blanks to define a custom member route named 'preview' for articles.

Ruby on Rails
Rails.application.routes.draw do
  resources :articles do
    [1] :preview, on: :[2]
  end
end
Drag options to blanks, or click blank then click option'
Amember
Bcollection
Cpost
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing member and collection routes.
Using wrong HTTP verbs.
5fill in blank
hard

Fill all three blanks to define nested routes for posts and comments with only 'create' and 'destroy' actions for comments.

Ruby on Rails
Rails.application.routes.draw do
  resources :posts do
    resources :comments, only: [:[1], :[2]], [3]: true
  end
end
Drag options to blanks, or click blank then click option'
Acreate
Bdestroy
Cshallow
Dindex
Attempts:
3 left
💡 Hint
Common Mistakes
Including unwanted actions in 'only'.
Forgetting to add 'shallow: true' for better route structure.

Practice

(1/5)
1. What does the resources :books line do in a Rails routes file?
easy
A. Creates all standard RESTful routes for the books resource automatically.
B. Creates only the index and show routes for books.
C. Creates a single custom route named books.
D. Deletes all routes related to books.

Solution

  1. Step 1: Understand the resources method

    The resources method in Rails automatically generates all seven standard RESTful routes (index, show, new, create, edit, update, destroy) for a given resource.
  2. Step 2: Apply to resources :books

    Using resources :books creates all these routes for the books controller, enabling standard CRUD actions.
  3. Final Answer:

    Creates all standard RESTful routes for the books resource automatically. -> Option A
  4. Quick Check:

    resources :books = all RESTful routes [OK]
Hint: Remember: resources creates all standard routes by default [OK]
Common Mistakes:
  • Thinking it creates only some routes
  • Confusing resources with resource (singular)
  • Assuming it deletes routes
2. Which of the following is the correct syntax to create only the index and show routes for a resource named articles?
easy
A. resources :articles, only: [:index, :show]
B. resources :articles, except: [:index, :show]
C. resource :articles, only: [:index, :show]
D. resources :articles, only: index, show

Solution

  1. Step 1: Use resources with only option

    To limit routes to specific actions, use only: [:index, :show] with resources.
  2. Step 2: Check syntax correctness

    resources :articles, only: [:index, :show] correctly uses symbols in an array with only. resources :articles, except: [:index, :show] uses except which excludes actions, not includes. resource :articles, only: [:index, :show] uses singular resource which is for single objects, not collections. resources :articles, only: index, show syntax is invalid because only expects an array.
  3. Final Answer:

    resources :articles, only: [:index, :show] -> Option A
  4. Quick Check:

    Limit routes with only and array [OK]
Hint: Use only: with an array of symbols to limit routes [OK]
Common Mistakes:
  • Using except instead of only
  • Using singular resource for collections
  • Incorrect syntax without brackets
3. Given the route declaration resources :users, except: [:destroy], which HTTP verb and path will NOT be available?
medium
A. GET /users/:id/edit
B. DELETE /users/:id
C. POST /users
D. GET /users

Solution

  1. Step 1: Understand except: [:destroy]

    The except option removes the specified action routes. Here, destroy is excluded, so no route for deleting a user is created.
  2. Step 2: Identify the HTTP verb and path for destroy

    The destroy action corresponds to the HTTP DELETE verb on the path /users/:id. This route will be missing.
  3. Final Answer:

    DELETE /users/:id -> Option B
  4. Quick Check:

    except: [:destroy] removes DELETE route [OK]
Hint: Destroy action uses DELETE verb; except removes it [OK]
Common Mistakes:
  • Confusing except with only
  • Thinking GET routes are removed
  • Mixing HTTP verbs with wrong actions
4. Identify the error in this routes file snippet:
resources :products, only: :show, :edit
medium
A. The only option should list all seven RESTful actions.
B. The resources method cannot use only option.
C. The resource name products should be singular.
D. The only option should be an array of symbols, not separate symbols.

Solution

  1. Step 1: Check syntax of only option

    The only option expects an array of symbols like only: [:show, :edit]. Writing only: :show, :edit passes :show as the only option and :edit is outside the hash, causing syntax error.
  2. Step 2: Verify other options

    resources supports only. The resource name should be plural for collections. Listing all actions is optional.
  3. Final Answer:

    The only option should be an array of symbols, not separate symbols. -> Option D
  4. Quick Check:

    only needs array syntax [OK]
Hint: Use brackets for multiple actions in only: [:show, :edit] [OK]
Common Mistakes:
  • Omitting brackets for multiple actions
  • Using singular resource for collections
  • Thinking only must list all actions
5. You want to create RESTful routes for a resource orders but only allow index, show, and create actions. Additionally, you want a custom member route cancel that responds to POST requests. Which routes file code is correct?
hard
A. resources :orders, except: [:edit, :update, :destroy] do\n post 'cancel', on: :member\nend
B. resources :orders, only: [:index, :show, :create] do\n get 'cancel', on: :collection\nend
C. resources :orders, only: [:index, :show, :create] do\n post 'cancel', on: :member\nend
D. resource :orders, only: [:index, :show, :create] do\n post 'cancel', on: :member\nend

Solution

  1. Step 1: Limit routes to index, show, create

    Use only: [:index, :show, :create] to restrict routes to these actions.
  2. Step 2: Add custom member route for cancel

    Use a block with post 'cancel', on: :member to add a POST route for cancel on individual orders.
  3. Step 3: Check other options

    resources :orders, only: [:index, :show, :create] do\n get 'cancel', on: :collection\nend uses GET and collection which is incorrect for cancel on a single order. resources :orders, except: [:edit, :update, :destroy] do\n post 'cancel', on: :member\nend uses except which is less clear and includes extra routes. resource :orders, only: [:index, :show, :create] do\n post 'cancel', on: :member\nend uses singular resource which is wrong for collections.
  4. Final Answer:

    resources :orders, only: [:index, :show, :create] do\n post 'cancel', on: :member\nend -> Option C
  5. Quick Check:

    Use only with member block for custom POST route [OK]
Hint: Use member block with post for single resource custom action [OK]
Common Mistakes:
  • Using collection instead of member for single resource
  • Using singular resource for collections
  • Using wrong HTTP verb for custom action
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