Skip to content

Linter Rule: Disallow output ERB tags with control flow

Rule: erb-no-output-control-flow

Description

Disallow using output ERB tags (<%=) for control flow statements like if, unless, case, while, etc. Control flow should be written with regular ERB tags (<% ... %>), since these do not produce output directly.

Rationale

Using <%= with control flow is typically a mistake or misunderstanding of ERB behavior. Output tags (<%=) are designed to render values into the HTML output, while control flow statements only affect execution and do not produce a value to render. This misuse can result in unexpected output, unnecessary blank spaces, or subtle bugs.

Reporting this as a warning can help developers catch likely mistakes while allowing flexibility for rare advanced cases.

Examples

✅ Good

erb
<% if condition %>
  Content here
<% end %>
erb
<%= user.name %>

🚫 Bad

erb
<%= if condition %>
Control flow statements like `if` should not be used with output tags. Use `<% if ... %>` instead. (erb-no-output-control-flow)
Content here <% end %>
erb
<%= unless user.nil? %>
Control flow statements like `unless` should not be used with output tags. Use `<% unless ... %>` instead. (erb-no-output-control-flow)
Welcome! <% end %>

References

Released under the MIT License.