RESTful resource routes help organize web app URLs to match common actions like viewing, creating, or deleting data. This makes your app easier to understand and use.
RESTful resource routes in Ruby on Rails
resources :items
This single line creates many routes for standard actions like index, show, new, create, edit, update, and destroy.
You replace items with the name of your resource, usually plural.
resources :books
resources :users, only: [:index, :show]
resources :comments, except: [:destroy]This code sets up all standard RESTful routes for articles in a Rails app.
It creates routes like:
- GET /articles (index)
- GET /articles/:id (show)
- GET /articles/new (new)
- POST /articles (create)
- GET /articles/:id/edit (edit)
- PATCH/PUT /articles/:id (update)
- DELETE /articles/:id (destroy)
Rails.application.routes.draw do resources :articles end
Use rails routes command in terminal to see all routes created by resources.
You can customize routes by adding only or except options to limit actions.
RESTful routes help keep your app organized and follow web standards.
RESTful resource routes create standard URLs for common actions automatically.
Use resources :name to generate all routes for a resource.
Customize routes with only or except to control available actions.
Practice
resources :books line do in a Rails routes file?Solution
Step 1: Understand the
Theresourcesmethodresourcesmethod in Rails automatically generates all seven standard RESTful routes (index, show, new, create, edit, update, destroy) for a given resource.Step 2: Apply to
Usingresources :booksresources :bookscreates all these routes for the books controller, enabling standard CRUD actions.Final Answer:
Creates all standard RESTful routes for the books resource automatically. -> Option AQuick Check:
resources :books= all RESTful routes [OK]
- Thinking it creates only some routes
- Confusing resources with resource (singular)
- Assuming it deletes routes
index and show routes for a resource named articles?Solution
Step 1: Use
To limit routes to specific actions, useresourceswithonlyoptiononly: [:index, :show]withresources.Step 2: Check syntax correctness
resources :articles, only: [:index, :show] correctly uses symbols in an array withonly. resources :articles, except: [:index, :show] usesexceptwhich excludes actions, not includes. resource :articles, only: [:index, :show] uses singularresourcewhich is for single objects, not collections. resources :articles, only: index, show syntax is invalid becauseonlyexpects an array.Final Answer:
resources :articles, only: [:index, :show] -> Option AQuick Check:
Limit routes withonlyand array [OK]
- Using except instead of only
- Using singular resource for collections
- Incorrect syntax without brackets
resources :users, except: [:destroy], which HTTP verb and path will NOT be available?Solution
Step 1: Understand
Theexcept: [:destroy]exceptoption removes the specified action routes. Here,destroyis 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 BQuick Check:
except: [:destroy] removes DELETE route [OK]
- Confusing except with only
- Thinking GET routes are removed
- Mixing HTTP verbs with wrong actions
resources :products, only: :show, :edit
Solution
Step 1: Check syntax of
Theonlyoptiononlyoption expects an array of symbols likeonly: [:show, :edit]. Writingonly: :show, :editpasses :show as the only option and :edit is outside the hash, causing syntax error.Step 2: Verify other options
resourcessupportsonly. The resource name should be plural for collections. Listing all actions is optional.Final Answer:
Theonlyoption should be an array of symbols, not separate symbols. -> Option DQuick Check:
onlyneeds array syntax [OK]
- Omitting brackets for multiple actions
- Using singular resource for collections
- Thinking only must list all actions
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?Solution
Step 1: Limit routes to index, show, create
Useonly: [:index, :show, :create]to restrict routes to these actions.Step 2: Add custom member route for cancel
Use a block withpost 'cancel', on: :memberto 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 singularresourcewhich is wrong for collections.Final Answer:
resources :orders, only: [:index, :show, :create] do\n post 'cancel', on: :member\nend -> Option CQuick Check:
Use only with member block for custom POST route [OK]
- Using collection instead of member for single resource
- Using singular resource for collections
- Using wrong HTTP verb for custom action
Ready to prove it?
Pass the test and this pattern moves unknown
