Bird
Raised Fist0
Ruby on Railsframework~8 mins

ERB template syntax in Ruby on Rails - Performance & Optimization

Choose your learning style10 modes available
Performance: ERB template syntax
MEDIUM IMPACT
ERB template syntax affects how HTML and Ruby code are combined and rendered on the server, impacting server response time and initial page load speed.
Rendering dynamic content in a Rails view
Ruby on Rails
<% @processed_items = @items.map { |item| expensive_method_call(item) } %>
<% @processed_items.each do |processed| %>
  <%= processed %>
<% end %>
Precomputing data in the controller or before rendering reduces template processing time.
📈 Performance Gainreduces server render time by 50% or more
Rendering dynamic content in a Rails view
Ruby on Rails
<% @items.each do |item| %>
  <%= expensive_method_call(item) %>
<% end %>
Calling expensive methods inside the template causes slow server rendering and delays page delivery.
📉 Performance Costblocks rendering for 100+ ms depending on method complexity
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Expensive Ruby calls inside ERBN/A (server-side)N/AN/A[X] Bad
Precomputed data passed to ERBN/A (server-side)N/AN/A[OK] Good
Rendering Pipeline
ERB templates are processed on the server where Ruby code is executed and HTML is generated before sending to the browser.
→Server-side Rendering
→Network Transfer
→Browser Parsing
⚠️ BottleneckServer-side Rendering (template processing and Ruby execution)
Core Web Vital Affected
LCP
ERB template syntax affects how HTML and Ruby code are combined and rendered on the server, impacting server response time and initial page load speed.
Optimization Tips
1Avoid running expensive Ruby code directly in ERB templates.
2Precompute data in controllers or helpers before rendering templates.
3Use caching for partial templates to reduce repeated rendering costs.
Performance Quiz - 3 Questions
Test your performance knowledge
What is a common performance issue when using ERB templates in Rails?
ARunning expensive Ruby code directly inside the template
BUsing HTML tags in the template
CServing static assets from CDN
DCaching partials
DevTools: Network and Performance panels
How to check: Use Network panel to measure server response time; use Performance panel to check time to first byte and LCP.
What to look for: Look for long server response times indicating slow ERB rendering; fast TTFB and LCP indicate good performance.

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

  1. Step 1: Understand ERB tags for output

    The <%= %> tag evaluates Ruby code and inserts the result into the HTML.
  2. Step 2: Compare with other tags

    <% %> runs Ruby code without output, <%# %> is a comment, and <%== %> is not standard ERB syntax.
  3. Final Answer:

    <%= %> -> Option C
  4. 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

  1. Step 1: Identify ERB tags for code execution

    The <% %> tag runs Ruby code but does not insert output into HTML.
  2. Step 2: Differentiate from output tags

    <%= %> outputs result, <%# %> is a comment, and <%== %> is invalid syntax.
  3. Final Answer:

    <% ruby_code %> -> Option D
  4. 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

    1. 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.
    2. Step 2: Predict the HTML output

      Each item is wrapped in <li> tags with uppercase text, so the list shows APPLE and BANANA.
    3. Final Answer:

      <ul><li>APPLE</li><li>BANANA</li></ul> -> Option A
    4. Quick Check:

      Loop with upcase output = <ul><li>APPLE</li><li>BANANA</li></ul> [OK]
    Hint: Remember <%= %> outputs evaluated Ruby code [OK]
    Common Mistakes:
    • Expecting original lowercase instead of upcase
    • Thinking <% %> outputs text
    • Confusing string literal with method call
    4. Identify the error in this ERB snippet:
    <ul>
      <% items.each do |item| %>
        <li><% item.upcase %></li>
      <% end %>
    </ul>
    medium
    A. Syntax error in the loop declaration
    B. Missing output tag; item.upcase won't display
    C. Extra closing tag <% end %> is invalid
    D. Using <%# %> instead of <% %> for code

    Solution

    1. Step 1: Check the ERB tags around item.upcase

      The code uses <% item.upcase %> which runs Ruby but does not output anything.
    2. Step 2: Understand output requirement

      To show the uppercase text, it should use <%= item.upcase %> instead.
    3. Final Answer:

      Missing output tag; item.upcase won't display -> Option B
    4. Quick Check:

      Use <%= %> to output values [OK]
    Hint: Use <%= %> to output, <% %> only runs code [OK]
    Common Mistakes:
    • Confusing <% %> with output tag
    • Thinking loop syntax is wrong
    • Believing extra end tag causes error
    5. You want to conditionally display a message in ERB only if user.admin? is true. Which snippet correctly does this?
    hard
    A. <% if user.admin? %>Welcome, admin!<% end %>
    B. <%= if user.admin? %>Welcome, admin!<% end %>
    C. <% if user.admin? %><% 'Welcome, admin!' %><% end %>
    D. <%= user.admin? ? 'Welcome, admin!' %>

    Solution

    1. 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 %>.
    2. 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 : '').
    3. Final Answer:

      <% if user.admin? %>Welcome, admin!<% end %> -> Option A
    4. Quick Check:

      Conditional literal text output = <% if %>...<% end %> [OK]
    Hint: Use <% if cond %> text <% end %> for conditional display [OK]
    Common Mistakes:
    • Using <% %> for text inside conditional (no output)
    • Incomplete ternary missing : false_expr (syntax error)
    • Using <%= if cond %> (invalid Ruby syntax)
    Recommended

    Ready to prove it?

    Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

    10 questions~5 mineasy hard