This code renders the '_user' partial and passes the @user object as a local variable named 'user'. The partial displays the user's name.
Execution Table
Step
Action
Data Passed
Partial Variable
Partial Output
1
Parent view calls render with partial 'user'
N/A
N/A
N/A
2
Pass locals: { user: @user }
@user object
user = @user
N/A
3
Partial '_user.html.erb' starts rendering
user = @user
user
N/A
4
Partial accesses user.name
user.name = 'Alice'
user
<p>Name: Alice</p>
5
Partial rendering completes
N/A
N/A
<p>Name: Alice</p>
6
Parent view includes partial output
N/A
N/A
Full view with <p>Name: Alice</p> included
💡 Partial rendering ends after outputting content using passed local data.
Variable Tracker
Variable
Start
After Step 2
After Step 4
Final
user
undefined
@user object
@user object with name 'Alice'
undefined after partial ends
Key Moments - 3 Insights
Why do we use 'locals' when rendering a partial?
To explicitly pass data as local variables to the partial. Instance variables like @user are available automatically, but 'locals' provides clarity, allows custom names, and passes non-instance data, as shown in execution_table step 2.
Can the partial access instance variables like @user directly without locals?
Yes, partials inherit instance variables (@user) automatically from the parent view and controller. Locals are used for explicit passing, local variables, or renaming, as shown in execution_table steps 2 and 3.
What happens if you forget to pass the required local variable?
The partial will raise an error because the variable is undefined when it tries to access it, as implied by the variable_tracker showing 'undefined' before locals are passed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'user' inside the partial at step 4?
AThe @user object passed from the parent view
BUndefined
CNil
DA new empty object
💡 Hint
Check the 'Partial Variable' and 'Data Passed' columns at step 4 in the execution_table.
At which step does the partial start rendering with the passed data?
AStep 1
BStep 2
CStep 3
DStep 5
💡 Hint
Look for when the partial '_user.html.erb' starts rendering in the execution_table.
If you omit 'locals: { user: @user }' when rendering, what will happen?
APartial renders normally with access to @user
BPartial raises an error due to undefined 'user'
CPartial renders but 'user.name' is empty string
DPartial ignores the missing variable and renders nothing
💡 Hint
Refer to key_moments about variable availability and variable_tracker showing 'undefined' before locals are passed.
Concept Snapshot
Passing data to partials in Rails:
Use render partial: 'name', locals: { var: value }
Partial accesses data via local variable (e.g., 'var')
Partials DO inherit instance variables automatically
Pass data explicitly with locals for clarity
This keeps views modular and clear
Full Transcript
In Rails, when you want to reuse a view snippet called a partial, you often pass it data via locals. The parent view calls render with the partial name and passes data using the locals hash. This makes local variables available inside the partial. Instance variables like @user are accessible automatically, but locals provide explicitness. The partial uses these variables to render dynamic content. Without locals, local vars like 'user' are undefined and cause errors if used. This trace shows passing @user as 'user' to the partial, which renders the name. This keeps views clean and manageable.