Skip to main content

ERB vs. Phlex: Choosing the Right View Strategy for Your Rails App

The Ruby on Rails community is currently at a crossroads regarding the “View” layer. On one side, we have ERB (Embedded Ruby), the battle-tested veteran that has powered Rails since day one. On the other, Phlex has emerged as a disruptive alternative that treats HTML as pure Ruby code.

Both approaches are valid, but they favor different mental models and workflows. Here is a breakdown of the pros and cons of each to help you decide which fits your project best.

1. ERB (Embedded Ruby)

ERB is a template-based system. It feels like writing an HTML document that has “holes” where Ruby code can be injected.

The Pros

  • HTML-First Mindset: Because it looks like HTML, it is very accessible to frontend developers and designers who may not be proficient in Ruby.
  • Standard Framework Support: Every Rails tool, tutorial, and gem is built with ERB in mind. It requires zero configuration.
  • Low Barrier to Entry: Beginners can start building views immediately without understanding Object-Oriented Programming (OOP) principles.

The Cons

  • Fragmentation: Logic often ends up scattered between .html.erb files, global helpers, and partials.
  • Loose Contracts: Partials don’t have a formal way to define required arguments, often leading to “undefined local variable” errors at runtime.
  • Performance: Parsing and interpolating large strings in ERB is slower than executing compiled Ruby methods.

2. Phlex

Phlex is a component-based system. It feels like writing a Ruby class that happens to output HTML.

The Pros

  • Object-Oriented Power: You get the full benefit of Ruby: private methods for refactoring, constants for CSS classes, and inheritance.
  • Type Safety & Contracts: By using the standard initialize method, you define exactly what data a component needs. If you miss an argument, Ruby tells you exactly why and where.
  • Speed: Phlex is optimized for high performance, often outperforming ERB and even other component gems like ViewComponent.
  • Developer Experience: Everything is in one place. You don’t have to jump between a helper file and a template to understand a component’s logic.

The Cons

  • The “Ruby Barrier”: Designers who only know HTML/CSS will find Phlex intimidating, as the HTML structure is abstracted into Ruby method calls.
  • Non-Standard Syntax: While it mirrors HTML, you are writing div { ... } instead of <div>...</div>. This requires a mental shift and can occasionally lead to syntax confusion.

The Side-by-Side: User Card Component

To see the difference in “feeling,” consider this comparison:

ERB Implementation

1
2
3
4
5
6
7
<div class="user-card">
  <%= image_tag @user.avatar %>
  <h2><%= @user.name %></h2>
  <% if @user.admin? %>
    <span class="badge">Admin</span>
  <% end %>
</div>

Phlex Implementation

1
2
3
4
5
6
7
def view_template
  div(class: "user-card") do
    image_tag(@user.avatar)
    h2 { @user.name }
    span(class: "badge") { "Admin" } if @user.admin?
  end
end

Which One Should You Choose?

Choose ERB if:

  • You work in a team with dedicated frontend developers or designers who need to edit templates directly.
  • You are building a content-heavy site where the HTML structure is more important than the logic.
  • You want to stick to the Rails default to ensure maximum compatibility with third-party gems.

Choose Phlex if:

  • You are a solo developer or a Ruby-heavy team that wants to stay within the Ruby language as much as possible.
  • You are building a complex, data-driven application with many reusable UI components.
  • Performance is a top priority for your rendering layer.
  • You find yourself frustrated by the “spaghetti” logic of helpers and partials.