0
0
Ruby on Railsframework~10 mins

Why routing maps URLs to actions in Ruby on Rails - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why routing maps URLs to actions
User enters URL
Rails router receives URL
Router matches URL pattern
Router finds controller & action
Controller action runs
Response sent back to user
This flow shows how Rails takes a URL, finds the right controller and action, runs it, and sends back a response.
Execution Sample
Ruby on Rails
get '/books/:id', to: 'books#show'

# User visits /books/5
# Router matches URL
# Calls BooksController#show with id=5
This code maps the URL /books/5 to the show action in BooksController with id parameter 5.
Execution Table
StepURL ReceivedRouter ActionControllerActionParameters
1/books/5Match pattern '/books/:id'BooksControllershow{id: '5'}
2Call actionExecute show methodBooksControllershow{id: '5'}
3Send responseRender view or JSONBooksControllershow{id: '5'}
💡 Routing ends after controller action runs and response is sent
Variable Tracker
VariableStartAfter Step 1After Step 2Final
url'''/books/5''/books/5''/books/5'
controllernilBooksControllerBooksControllerBooksController
actionnilshowshowshow
params{}{id: '5'}{id: '5'}{id: '5'}
Key Moments - 2 Insights
Why does the router need to match the URL pattern before calling the controller?
The router uses the URL pattern to know which controller and action to run. Without matching, it wouldn't know what code to execute. See execution_table step 1.
How does the router pass the id '5' to the controller?
The router extracts '5' from the URL and puts it in params as id: '5'. The controller action can then use params[:id]. See variable_tracker for params changes.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what controller handles the URL '/books/5'?
AAuthorsController
BBooksController
CPagesController
DHomeController
💡 Hint
Check the 'Controller' column in execution_table step 1
At which step does the router extract parameters from the URL?
AStep 2
BStep 3
CStep 1
DAfter all steps
💡 Hint
Look at the 'Parameters' column in execution_table; params appear at step 1
If the URL was '/books/10', how would params change in variable_tracker?
A{id: '10'}
B{}
C{id: '5'}
Dnil
💡 Hint
Params reflect the id from the URL, see variable_tracker params row
Concept Snapshot
Rails routing connects URLs to controller actions.
The router matches URL patterns like '/books/:id'.
It extracts parameters (like id) from the URL.
Then it calls the matching controller and action.
The action runs and sends a response back.
This lets URLs trigger specific code easily.
Full Transcript
When a user types a URL, Rails routing takes that URL and looks for a pattern it knows. For example, '/books/5' matches the pattern '/books/:id'. The router then knows to call the 'show' action in the BooksController and passes the id parameter with value '5'. The controller runs this action, which usually finds the book with id 5 and sends back a page or data. This process connects web addresses to the right code to run.