0
0
Ruby on Railsframework~10 mins

RESTful resource routes in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - RESTful resource routes
Client sends HTTP request
Rails router matches URL + HTTP verb
Select controller and action
Controller processes request
Render view or redirect
Send HTTP response back to client
Rails uses the URL and HTTP method to pick the right controller action, which then handles the request and sends back a response.
Execution Sample
Ruby on Rails
resources :books

# GET /books -> books#index
# POST /books -> books#create
# GET /books/:id -> books#show
# PATCH /books/:id -> books#update
# DELETE /books/:id -> books#destroy
Defines standard RESTful routes for a 'books' resource, mapping HTTP verbs and URLs to controller actions.
Execution Table
StepHTTP VerbURLController#ActionPurpose
1GET/booksbooks#indexShow list of all books
2GET/books/newbooks#newShow form to create a new book
3POST/booksbooks#createCreate a new book
4GET/books/:idbooks#showShow details of one book
5GET/books/:id/editbooks#editShow form to edit a book
6PATCH/books/:idbooks#updateUpdate a book
7PUT/books/:idbooks#updateUpdate a book (alternative)
8DELETE/books/:idbooks#destroyDelete a book
9---No more routes, routing ends
💡 All standard RESTful routes for 'books' resource are matched and handled.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 6Final
params[:id]nilnilnil4242
request.methodnilGETPOSTPATCHPATCH
controller_actionnilbooks#indexbooks#createbooks#updatebooks#update
Key Moments - 3 Insights
Why does the same URL /books/:id respond to different actions?
Because Rails uses the HTTP verb (GET, PATCH, DELETE) along with the URL to decide which controller action to run, as shown in execution_table rows 4, 6, and 8.
What happens if I send a POST request to /books/:id?
There is no route for POST with /books/:id, so Rails will return a routing error. Only POST /books (row 3) is valid for creating.
Why are there both PATCH and PUT routes for update?
Rails supports both PATCH and PUT for updating resources (rows 6 and 7), but PATCH is preferred for partial updates.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, which controller action handles GET /books/5/edit?
Abooks#show
Bbooks#edit
Cbooks#update
Dbooks#new
💡 Hint
Check row 5 in the execution_table for GET /books/:id/edit
At which step does the controller action books#create get called?
AStep 2
BStep 4
CStep 3
DStep 6
💡 Hint
Look for POST /books in the execution_table
If you send a DELETE request to /books/10, which action runs and what is params[:id]?
Abooks#destroy, params[:id] = 10
Bbooks#show, params[:id] = 10
Cbooks#destroy, params[:id] = nil
DRouting error
💡 Hint
See row 8 in execution_table and variable_tracker for params[:id]
Concept Snapshot
RESTful resource routes in Rails map HTTP verbs and URLs to controller actions.
Use 'resources :name' to create 7 standard routes.
GET for reading, POST for creating, PATCH/PUT for updating, DELETE for deleting.
URLs include optional :id for single resource actions.
Rails router matches verb + URL to pick controller#action.
This keeps routes organized and predictable.
Full Transcript
In Rails, RESTful resource routes connect HTTP methods and URLs to specific controller actions. When a client sends a request, Rails looks at the URL and the HTTP verb to find the matching route. For example, GET /books calls the books#index action to list all books. POST /books calls books#create to add a new book. URLs with an :id like /books/42 target a single book for showing, editing, updating, or deleting. PATCH and PUT both update a book, but PATCH is preferred for partial updates. This system helps keep your app's routes clear and easy to understand.