📖 Scenario: You are building a simple webpage for a small bakery. You want to display a welcome message and a list of your top 3 favorite pastries using Ruby on Rails with ERB templates.
🎯 Goal: Create an ERB template that shows a greeting message and dynamically lists the pastry names using ERB syntax.
📋 What You'll Learn
Create a Ruby array called pastries with exactly these strings: "Croissant", "Eclair", "Macaron"
Create a string variable called greeting with the exact value "Welcome to Sweet Treats Bakery!"
Use ERB output tags <%= %> to display the greeting inside an <h1> tag
Use ERB control flow tags <% %> with a for loop to iterate over pastries and display each pastry inside an <li> tag within a <ul>
💡 Why This Matters
🌍 Real World
ERB templates are used in Ruby on Rails to create dynamic HTML pages that change based on data from Ruby code.
💼 Career
Understanding ERB syntax is essential for Rails developers to build interactive and data-driven web applications.
Progress0 / 4 steps
1
Create the pastries array
Create a Ruby array called pastries with these exact strings: "Croissant", "Eclair", "Macaron"
Ruby on Rails
Hint
Use square brackets [] to create an array and separate items with commas.
2
Create the greeting string
Create a string variable called greeting with the exact value "Welcome to Sweet Treats Bakery!"
Ruby on Rails
Hint
Use double quotes "" to create the string exactly as shown.
3
Display the greeting with ERB output tag
Use an ERB output tag <%= greeting %> inside an <h1> tag to display the greeting variable.
Ruby on Rails
Hint
Use <%= %> to output Ruby variables in ERB templates.
4
List pastries using ERB for loop
Use an ERB control flow tag <% for pastry in pastries %> to loop over pastries and display each pastry inside an <li> tag within a <ul>. Close the loop with <% end %>.
Ruby on Rails
Hint
Use <% %> for Ruby code that does not output directly, and <%= %> to output values inside the loop.
Practice
(1/5)
1. Which ERB tag should you use to display the result of a Ruby expression inside HTML?
easy
A. <% %>
B. <%# %>
C. <%= %>
D. <%== %>
Solution
Step 1: Understand ERB tags for output
The <%= %> tag evaluates Ruby code and inserts the result into the HTML.
Step 2: Compare with other tags
<% %> runs Ruby code without output, <%# %> is a comment, and <%== %> is not standard ERB syntax.
Final Answer:
<%= %> -> Option C
Quick Check:
Output Ruby result = <%= %> [OK]
Hint: Use <%= %> to show Ruby results in HTML [OK]
Common Mistakes:
Using <% %> which does not output anything
Confusing comment tag <%# %> with output
Trying to use <%== %> which is invalid
2. Which of the following is the correct syntax to run Ruby code without showing output in an ERB template?
easy
A. <%== ruby_code %>
B. <%= ruby_code %>
C. <%# ruby_code %>
D. <% ruby_code %>
Solution
Step 1: Identify ERB tags for code execution
The <% %> tag runs Ruby code but does not insert output into HTML.
Step 2: Differentiate from output tags
<%= %> outputs result, <%# %> is a comment, and <%== %> is invalid syntax.
Final Answer:
<% ruby_code %> -> Option D
Quick Check:
Run code without output = <% %> [OK]
Hint: Use <% %> for Ruby logic without output [OK]
Common Mistakes:
Using <%= %> which outputs result
Using <%# %> which comments out code
Trying invalid <%== %> syntax
3. Given this ERB snippet:
<ul>
<% items.each do |item| %>
<li><%= item.upcase %></li>
<% end %>
</ul>
What will be the rendered HTML if items = ['apple', 'banana']?
medium
A.
APPLE
BANANA
B.
item.upcase
item.upcase
C.
apple
banana
D.
Solution
Step 1: Understand the loop and method call
The code loops over each item in the array and outputs the uppercase version using item.upcase.
Step 2: Predict the HTML output
Each item is wrapped in <li> tags with uppercase text, so the list shows APPLE and BANANA.
Final Answer:
<ul><li>APPLE</li><li>BANANA</li></ul> -> Option A
Quick Check:
Loop with upcase output = <ul><li>APPLE</li><li>BANANA</li></ul> [OK]