0
0
Ruby on Railsframework~10 mins

Route parameters in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Route parameters
User sends HTTP request
Rails router matches URL pattern
Extract route parameters from URL
Pass parameters to controller action
Controller uses parameters to find data
Render response with data
Rails takes the URL, finds matching route, extracts parameters from it, and sends them to the controller to use.
Execution Sample
Ruby on Rails
get '/books/:id', to: 'books#show'

# URL: /books/42
# params[:id] will be '42'
This route captures the :id part from the URL and makes it available in params for the controller.
Execution Table
StepURL RequestedRoute Pattern MatchedExtracted ParamsController ActionAction Parameter Value
1/books/42/books/:id{ id: '42' }books#showparams[:id] = '42'
2/books/7/books/:id{ id: '7' }books#showparams[:id] = '7'
3/booksNo match{}No actionNo params
4/books/abc/books/:id{ id: 'abc' }books#showparams[:id] = 'abc'
💡 Routing stops when no matching route pattern is found or after controller action is called.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
params[:id]nil'42''7'nil'abc'
Key Moments - 2 Insights
Why does params[:id] have different values for different URLs?
Because the router extracts the part of the URL that matches :id in the route pattern, as shown in execution_table rows 1, 2, and 4.
What happens if the URL does not match the route pattern?
No route is matched, so no params are extracted and no controller action is called, as shown in execution_table row 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is params[:id] when the URL is '/books/7'?
Anil
B'7'
C'42'
D'abc'
💡 Hint
Check execution_table row 2 under 'Action Parameter Value'
At which step does the router fail to find a matching route?
AStep 3
BStep 1
CStep 4
DStep 2
💡 Hint
Look at execution_table row 3 under 'Route Pattern Matched'
If the route pattern changed to '/books/:book_id', what would params key be for the URL '/books/42'?
Aparams[:id]
Bparams[:42]
Cparams[:book_id]
Dparams[:book]
💡 Hint
Route parameter names come from the pattern segment after the colon, see concept_flow
Concept Snapshot
Rails route parameters capture parts of the URL defined by :name in routes.
These parameters become keys in the params hash.
Controllers access params[:name] to get values.
If URL doesn't match route, no params are set.
Example: get '/books/:id' makes params[:id] available.
Full Transcript
In Rails, when a user sends a request URL, the router looks for a matching route pattern. If the pattern includes parts like :id, Rails extracts that part from the URL and puts it in params with the key 'id'. The controller action then uses params[:id] to find or show data. If the URL does not match any route, no parameters are extracted and no controller action runs. For example, the route get '/books/:id' matches URLs like /books/42 and sets params[:id] to '42'. This process helps Rails know what data the user wants based on the URL.