Nice work. We added a controller and a route to the Rails app.
Why does the Messages controller use an action named
index? Check out the diagram in the browser. Rails provides seven standard controller actions for doing common operations with data. Here we want display a list of all messages, so we used the
Putting it all together:
http://localhost:8000/messages, the routes file maps this request to the Messages controller’s
indexaction retrieves all messages from the database and stores them in variable
@messages variable is passed on to the view. The view should display each message, so let’s set it up next.
Open app/views/messages/index.html.erb. Under line 11, type the contents as you see here:
<% @messages.each do |message| %> <div class="message"> <p class="content"><%= message.content %></p> <p class="time"><%= message.created_at %></p> </div> <% end %>
http://localhost:8000/messages in the browser.