0
0
Ruby on Railsframework~3 mins

Why ERB template syntax in Ruby on Rails? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

Discover how ERB turns tangled code into clean, dynamic web pages effortlessly!

The Scenario

Imagine writing a web page where you have to mix HTML and Ruby code by hand, inserting Ruby output manually into the HTML file.

The Problem

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.

The Solution

ERB template syntax lets you embed Ruby code directly inside HTML using simple tags, so the server can generate dynamic pages cleanly and safely.

Before vs After
Before
<html>
  <body>
    <p>Hello, #{user.name}!</p>
  </body>
</html>
After
<p>Hello, <%= user.name %>!</p>
What It Enables

It enables creating dynamic web pages that update content based on Ruby code without messy manual string building.

Real Life Example

Showing a personalized greeting on a website that changes depending on who is logged in.

Key Takeaways

ERB cleanly mixes Ruby and HTML.

It avoids manual string concatenation.

It makes dynamic web pages easy to build and maintain.