Nice work! The app now displays a list of all messages in the database. How does this work?
The file index.html.erb is a web template. Web templates are HTML files that contain variables and control flow statements. Rather than write the same HTML over and over again for each message, we can use web templates to loop through and display data from the database.
In this case:
<% @messages.each do |message| %> iterates through each message in
@messages array. We created
@messages in the Messages controller's
- For each message, we use
<%= message.content %> and
<%= message.created_at %> to display its content and the time when it was created.
The default web templating language in Rails is embedded Ruby, or ERB.