Blog

MVC: View

Placeholder Avatar
Rachelle LeQuesne
February 13, 2017

Note: This post is intended as a supplement to WTF Just Happened? A Quick Tour of Your First Rails App.

MVC: View

A view in Rails is a template for a HTML document. This template has the extension *.html.erb. The .erb part means that it may include embedded Ruby code.

The view template contains the HTML structure together with Ruby code that is used to populate certain fields with data. We use the tags <%= or <% and %> to indicate the beginning and the end of the embedded Ruby code.

<% means that the enclosed Ruby code is a script that should be executed. <%= means that the enclosed Ruby code is an expression that should be evaluated. Below is the standard index template (list of all posts) provided for you by Rails when you run rails generate scaffold Post.

```ruby

<%= notice %>

Posts

<% @posts.each do |post| %> <% end %>
Title Body
<%= post.title %> <%= post.body %> <%= link_to 'Show', post %> <%= link_to 'Edit', edit_post_path(post) %> <%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %>


<%= link_to ‘New Post’, new_post_path %> ```