0
0
Ruby on Railsframework~10 mins

Why controllers handle requests in Ruby on Rails - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why controllers handle requests
Browser sends HTTP request
Rails router receives request
Router finds matching controller & action
Controller action runs
Controller processes data & prepares response
Controller sends response back to browser
This flow shows how a web request travels from the browser to the Rails controller, which handles it and sends back a response.
Execution Sample
Ruby on Rails
class BooksController < ApplicationController
  def show
    @book = Book.find(params[:id])
    render json: @book
  end
end
This controller action finds a book by ID and sends it back as JSON when a request comes in.
Execution Table
StepActionInput/StateOutput/Result
1Browser sends GET /books/3URL: /books/3Request received by Rails router
2Router matches routeRoute: GET /books/:idCalls BooksController#show with params[:id] = 3
3Controller action startsparams[:id] = 3Finds Book with id=3
4Book foundBook object with id=3Assigns to @book
5Render JSON response@book assignedSends JSON of book data to browser
6Response sentJSON dataBrowser receives book info
7EndRequest handledNo further action
💡 Request handled fully by controller; response sent back to browser
Variable Tracker
VariableStartAfter Step 3After Step 4Final
params[:id]nil333
@booknilnilBook(id=3)Book(id=3)
Key Moments - 3 Insights
Why does the router send the request to the controller?
The router matches the URL pattern to a controller and action, so it knows which controller method should handle the request (see execution_table step 2).
Why does the controller find the book instead of the router?
The controller handles the logic and data fetching to keep routing simple and focused on directing requests (see execution_table step 3 and 4).
What does the controller do after finding the book?
It prepares the response, here rendering the book as JSON, and sends it back to the browser (see execution_table step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of params[:id] at step 3?
A3
Bnil
CBook object
DJSON data
💡 Hint
Check the 'Input/State' column at step 3 in the execution_table.
At which step does the controller send the response back to the browser?
AStep 4
BStep 2
CStep 5
DStep 1
💡 Hint
Look for the step where 'Render JSON response' happens in the execution_table.
If the router did not find a matching route, what would happen?
AThe controller action runs anyway
BThe browser gets a 404 error
CThe book is found by default
DThe router sends a JSON response
💡 Hint
Think about what happens if the router cannot match the URL to any controller (not shown in table but common Rails behavior).
Concept Snapshot
In Rails, controllers handle requests by receiving data from the router.
They run actions to process data and prepare responses.
Controllers keep routing simple and manage app logic.
They send the final response back to the browser.
This separation keeps code organized and clear.
Full Transcript
When a browser sends a request, Rails first uses the router to find which controller and action should handle it. The controller then runs the action method, using parameters from the request to find or process data. After preparing the data, the controller sends a response back to the browser, such as rendering JSON or HTML. This flow keeps routing focused on directing requests, while controllers manage the app's logic and responses.