This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
- Introduction 2:03
- Creating a Route 4:45
- Creating a Route 4 questions
- Viewing Routes 1 objective
- Adding a Route 1 objective
- Creating a Controller 2:39
- Creating a Controller 1 objective
- Creating a View 1:29
- Creating a View 1 question
- Creating a Model 3:32
- Creating a Model 1 objective
- Adding Records via Rails Console 3:17
- Populating the View 8:57
- Populating the View 2 objectives

- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
We've set up our controller's "index" method to load all "Page" objects into the "@pages" instance variable. We've also created some "Page" records for it to load. That was only part of the problem, though; we still don't see any page data. That's because we haven't set up the our view template to display the pages we've loaded. To do that, we're going to use ERB to embed some Ruby code into our HTML.
Most text in an ERB template is copied to the output verbatim.
<p>I'll appear in the output exactly as I do here.</p>
But Ruby code within ERB tags gets evaluated instead of being copied directly to the output.
Code within output ERB tags (<%= %>
) is evaluated, and the result is embedded into the output. This code will output the current time:
<p>The current time is: <%= Time.now %></p>
Code within regular ERB tags (<% %>
, without an equals sign) is also evaluated, but is not included directly in the output. It can be used to influence the result of output tags.
If you place a conditional within regular tags, text within the conditional will only be output if the condition is true. This code will include You passed!
in the output:
<% grade = 97 %>
<% if grade > 60 %>
You passed!
<% end %>
This code will not:
<% grade = 54 %>
<% if grade > 60 %>
You passed!
<% end %>
If you place a loop in regular ERB tags, text within the loop will be output repeatedly. This code will output 4 <p>
elements with the text 0 fish
, 1 fish
, 2 fish
, and 3 fish
:
<% 4.times do |index| %>
<p><%= index %> fish</p>
<% end %>
Assuming @pages
contains a collection of Page
objects, this code will output the title of each Page
:
<% @pages.each do |page| %>
<p>
<%= page.title %>
</p>
<% end %>
Want to learn about looping over the values in a collection with each
? Check out our Ruby Loops course.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
gene c
13,630 Points1 Answer
-
SOS Norway
4,219 Points2 Answers
-
Ian Friedel
11,960 PointsPopulating the View video for Rails Routes and Resources is not loading because of an 'error loading this resource'
Posted by Ian FriedelIan Friedel
11,960 Points1 Answer
-
Yuichi Narisawa
19,548 Points1 Answer
-
Oğulcan Girginc
24,848 Points1 Answer
View all discussions for this video
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up