0
0
Ruby on Railsframework~10 mins

Why views present data in Ruby on Rails - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why views present data
Controller gets data
Controller sends data to View
View receives data
View formats and displays data
User sees the data on the screen
The controller fetches data and sends it to the view, which then formats and shows it to the user.
Execution Sample
Ruby on Rails
def show
  @user = User.find(params[:id])
end

# In show.html.erb
<p><%= @user.name %></p>
This code gets a user from the database and the view shows the user's name.
Execution Table
StepActionData StateView Output
1Controller receives request with user idparams[:id] = 3No output yet
2Controller finds user by id@user = User(id:3, name:'Alice')No output yet
3Controller passes @user to view@user available in viewNo output yet
4View reads @user.nameAccess 'Alice'Prepares to display 'Alice'
5View renders HTML with user nameData unchanged<p>Alice</p>
6User sees rendered HTMLData unchangedAlice shown on page
💡 View finishes rendering and user sees the data on the screen
Variable Tracker
VariableStartAfter Step 2After Step 3Final
params[:id]nil333
@usernilUser(id:3, name:'Alice')User(id:3, name:'Alice')User(id:3, name:'Alice')
Key Moments - 2 Insights
Why does the view need the controller to send data?
The view only shows data; it does not get data itself. The controller fetches data and sends it to the view, as shown in steps 2 and 3 of the execution table.
Can the view change the data it receives?
No, the view only formats and displays data. It does not modify the data, which stays the same from step 3 to the final step.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of @user after step 2?
AUser(id:3, name:'Alice')
Bnil
Cparams[:id]
D3
💡 Hint
Check the 'Data State' column at step 2 in the execution table.
At which step does the view start rendering the user name?
AStep 5
BStep 3
CStep 4
DStep 6
💡 Hint
Look for when the view reads @user.name in the execution table.
If the controller did not find a user, what would the view receive?
A@user with user data
Bnil or empty @user
Cparams[:id]
DAn error message
💡 Hint
Think about what happens if User.find fails before passing data to the view.
Concept Snapshot
In Rails, the controller gets data from the model.
It sends this data to the view.
The view only shows the data, it does not fetch or change it.
This separation keeps code organized and clear.
Example: controller sets @user, view shows @user.name.
Full Transcript
In Rails, when a user requests a page, the controller first gets the needed data, like a user record from the database. Then, the controller sends this data to the view. The view's job is to take this data and turn it into HTML that the user can see. The view does not get data by itself or change it; it only presents it. This keeps the app organized: controllers handle data, views handle display. For example, the controller finds a user by id and sets @user. The view then uses @user.name to show the user's name on the page.