Goal

You’ll be able to write basic ERB templates, and understand the genuinely important distinction between the two ERB tag types, since confusing them causes a real, common bug.

Learn

ERB (Embedded Ruby) lets you embed Ruby code directly inside HTML templates. There are two distinct tag types, and the difference between them genuinely matters:

<%= expression %>    <!-- outputs the result into the HTML -->
<% code %>            <!-- runs the code, but outputs NOTHING -->

<%= %> (with the equals sign) evaluates the Ruby expression and inserts its result directly into the rendered HTML. <% %> (no equals sign) runs the Ruby code but doesn’t output anything — used for control flow like loops and conditionals, where you don’t want the loop/if statement itself to appear as text in the page:

<% @products.each do |product| %>
  <p><%= product.name %></p>
<% end %>

Notice the loop structure itself uses the no-output <% %> tags (you don’t want the word “each” or “end” appearing on the page), while product.name — the actual value you want displayed — uses the output <%= %> tag.

A genuinely common beginner mistake is using the wrong tag type — using <% %> when you actually wanted to display a value (nothing appears, confusingly), or using <%= %> around a loop’s start (Ruby raises a syntax error, or worse, displays something unintended, since the loop construct itself isn’t a value meant to be output).

Decision Task

You write <% @product.name %> in a view, expecting the product’s name to appear on the page, but nothing shows up at all. Before reading on: what’s the specific mistake here?

Show Answer

Missing the equals sign — <% %> runs the code but doesn’t output the result; you need <%= @product.name %> (with the equals sign) to actually insert the value into the rendered HTML. This is exactly the common tag-confusion mistake this lesson specifically warns about.

Common Mistake

Confusing the two ERB tag types — using the non-outputting <% %> when you actually want a value displayed on the page, or using the outputting <%= %> around control-flow code like a loop or if statement, where you don’t want the construct itself treated as a value to display. Getting this backwards is one of the most common early ERB mistakes.

Practice Questions

1. Write the correct ERB tag to output a variable called @user.email onto the page.

Show Answer

<%= @user.email %>

2. Write the correct ERB tags for a loop that iterates over @orders and displays each order’s total.

Show Answer

<% @orders.each do |order| %>\n <%= order.total %>\n<% end %>

3. Why does the loop’s do/end structure use <% %> rather than <%= %>?

Show Answer

Because the loop construct itself isn’t a value meant to be displayed on the page — only the actual data extracted inside the loop (like order.total) should use the outputting tag.

4. True or False: <% %> and <%= %> behave identically, just with slightly different syntax.

Show Answer

False — <%= %> outputs its result into the HTML; <% %> runs the code but produces no visible output at all, a genuinely important functional difference, not just stylistic.

5. What would likely happen if you wrote <%= @orders.each do |order| %> instead of <% @orders.each do |order| %>?

Show Answer

This would likely cause unexpected output or an error, since the outputting tag would attempt to render the loop construct itself (or its return value) as displayed content, rather than just running it silently for its side effects.

Try It Yourself

Without looking back, write a complete ERB snippet that loops over @comments and displays each comment’s text inside a paragraph tag, using the correct tag types for each part.

Show Answer

<% @comments.each do |comment| %>\n <p><%= comment.text %></p>\n<% end %> — the loop structure uses non-outputting tags, while the actual displayed value uses the outputting tag.

Quick Check

1. What does <%= %> do?

Show Answer

Evaluates the Ruby expression and inserts its result into the rendered HTML.

2. What does <% %> do?

Show Answer

Runs the Ruby code but produces no visible output.

3. Which tag type would you use for a loop’s do/end structure?

Show Answer

<% %> (no equals sign, no output needed for the loop construct itself).

4. Which tag type would you use to display an actual value, like a product’s name?

Show Answer

<%= %> (with the equals sign).

5. What stands for “Embedded Ruby,” the templating system covered in this lesson?

Show Answer

ERB

تحميل هذا الباب / Download this Chapterنسخة كاملة للدراسة بدون إنترنت، مع الأسئلة والإجابات والصور المتاحة.