Discover how ERB turns tangled code into clean, dynamic web pages effortlessly!
Why ERB template syntax in Ruby on Rails? - Purpose & Use Cases
Imagine writing a web page where you have to mix HTML and Ruby code by hand, inserting Ruby output manually into the HTML file.
Manually mixing Ruby and HTML is confusing, easy to break, and hard to maintain because you must remember where to switch between code and markup.
ERB template syntax lets you embed Ruby code directly inside HTML using simple tags, so the server can generate dynamic pages cleanly and safely.
<html>
<body>
<p>Hello, #{user.name}!</p>
</body>
</html><p>Hello, <%= user.name %>!</p>
It enables creating dynamic web pages that update content based on Ruby code without messy manual string building.
Showing a personalized greeting on a website that changes depending on who is logged in.
ERB cleanly mixes Ruby and HTML.
It avoids manual string concatenation.
It makes dynamic web pages easy to build and maintain.
Practice
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 CQuick Check:
Output Ruby result = <%= %> [OK]
- Using <% %> which does not output anything
- Confusing comment tag <%# %> with output
- Trying to use <%== %> which is invalid
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 DQuick Check:
Run code without output = <% %> [OK]
- Using <%= %> which outputs result
- Using <%# %> which comments out code
- Trying invalid <%== %> syntax
<ul>
<% items.each do |item| %>
<li><%= item.upcase %></li>
<% end %>
</ul>What will be the rendered HTML if
items = ['apple', 'banana']?Solution
Step 1: Understand the loop and method call
The code loops over each item in the array and outputs the uppercase version usingitem.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 AQuick Check:
Loop with upcase output = <ul><li>APPLE</li><li>BANANA</li></ul> [OK]
- Expecting original lowercase instead of upcase
- Thinking <% %> outputs text
- Confusing string literal with method call
<ul>
<% items.each do |item| %>
<li><% item.upcase %></li>
<% end %>
</ul>Solution
Step 1: Check the ERB tags around item.upcase
The code uses<% item.upcase %>which runs Ruby but does not output anything.Step 2: Understand output requirement
To show the uppercase text, it should use<%= item.upcase %>instead.Final Answer:
Missing output tag; item.upcase won't display -> Option BQuick Check:
Use <%= %> to output values [OK]
- Confusing <% %> with output tag
- Thinking loop syntax is wrong
- Believing extra end tag causes error
user.admin? is true. Which snippet correctly does this?Solution
Step 1: Understand conditional rendering in ERB
<% if user.admin? %>starts an if block where literal text like "Welcome, admin!" is rendered only if true, ended by<% end %>.Step 2: Why others fail
B: Invalid syntax (<%= if %> can't start control flow like that). C: Inner <% %> runs code but outputs nothing. D: Incomplete ternary (missing: '').Final Answer:
<% if user.admin? %>Welcome, admin!<% end %> -> Option AQuick Check:
Conditional literal text output = <% if %>...<% end %> [OK]
- Using <% %> for text inside conditional (no output)
- Incomplete ternary missing : false_expr (syntax error)
- Using <%= if cond %> (invalid Ruby syntax)
Ready to prove it?
Pass the test and this pattern moves unknown
