It automatically creates standard routes for common actions like showing, creating, updating, and deleting resources, following REST principles.
Click to reveal answer
beginner
Name the 7 standard RESTful actions Rails generates for a resource.
index, show, new, create, edit, update, destroy
Click to reveal answer
beginner
What HTTP verb and path does the 'create' action use in RESTful routes?
POST request to /resources (e.g., POST /articles)
Click to reveal answer
beginner
How do you define RESTful routes for a resource named 'books' in Rails?
Use resources :books in the config/routes.rb file.
Click to reveal answer
beginner
What URL and HTTP verb does the 'edit' action use for a resource with id 5?
GET request to /resources/5/edit (e.g., GET /books/5/edit)
Click to reveal answer
Which HTTP verb is used to update a resource in RESTful routes?
APATCH
BGET
CPOST
DDELETE
✗ Incorrect
The PATCH verb is used to update an existing resource partially or fully.
What route helper does Rails generate for the 'show' action of a resource named 'posts'?
Aposts_path
Bpost_path(id)
Cnew_post_path
Dedit_post_path(id)
✗ Incorrect
The 'show' action uses post_path(id) to show a single post.
Which RESTful action corresponds to the URL '/articles/new'?
Aindex
Bshow
Cnew
Dedit
✗ Incorrect
The '/articles/new' URL shows the form to create a new article.
What does the 'destroy' action do in RESTful routes?
ADeletes a resource
BShows a resource
CCreates a resource
DEdits a resource
✗ Incorrect
The 'destroy' action deletes a resource from the database.
How do you limit RESTful routes to only 'index' and 'show' actions in Rails?
Aresource :items, only: [:index, :show]
Bresources :items, except: [:index, :show]
Cresources :items, limit: [:index, :show]
Dresources :items, only: [:index, :show]
✗ Incorrect
Use the 'only' option to specify which actions to generate routes for.
Explain how RESTful resource routes in Rails map HTTP verbs and URLs to controller actions.
Think about how Rails uses 'resources' to create these routes automatically.
You got /3 concepts.
Describe how you would customize RESTful routes in Rails to only include certain actions.
Look at the options you can pass to 'resources' in routes.rb.
You got /3 concepts.
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
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.
Step 2: Apply to resources :books
Using resources :books creates all these routes for the books controller, enabling standard CRUD actions.
Final Answer:
Creates all standard RESTful routes for the books resource automatically. -> Option A
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
Step 1: Use resources with only option
To limit routes to specific actions, use only: [:index, :show] with resources.
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.
Final Answer:
resources :articles, only: [:index, :show] -> Option A
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
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.
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.
Final Answer:
DELETE /users/:id -> Option B
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
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.
Step 2: Verify other options
resources supports only. The resource name should be plural for collections. Listing all actions is optional.
Final Answer:
The only option should be an array of symbols, not separate symbols. -> Option D
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
Step 1: Limit routes to index, show, create
Use only: [:index, :show, :create] to restrict routes to these actions.
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.
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.
Final Answer:
resources :orders, only: [:index, :show, :create] do\n post 'cancel', on: :member\nend -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.