Skip to content

Linter Rule: Disallow silent ERB statements

Rule: erb-no-silent-statement

Description

Disallow silent ERB tags (<% %>) that execute statements whose return value is discarded. Logic like method calls should live in controllers, helpers, or presenters, not in views. Assignments are allowed since they are pragmatic for DRYing up templates.

Control-flow keywords (next, break, redo, return, raise, and their if/unless modifier forms) are allowed since they have no return value to discard and cannot be moved out of the template. View-to-layout helpers (content_for, provide, and other designated side-effect helpers) are also allowed, matching erb-no-unused-expressions.

Rationale

Silent ERB tags that aren't control flow or assignments are a code smell. They execute Ruby code whose return value is silently discarded, which usually means the logic belongs in a controller, helper, or presenter rather than the view.

Examples

✅ Good

erb
<%= title %>
<%= render "partial" %>
erb
<% x = 1 %>
<% @title = "Hello" %>
<% x ||= default_value %>
<% x += 1 %>
erb
<% if user.admin? %>
  Admin tools
<% end %>
erb
<% users.each do |user| %>
  <p><%= user.name %></p>
<% end %>
erb
<% users.each do |user| %>
  <% next if user.hidden? %>
  <% break if reached_limit? %>
  <p><%= user.name %></p>
<% end %>
erb
<% content_for(:title, "Dashboard") %>
<% provide(:sidebar, render("sidebar")) %>

🚫 Bad

erb
<% some_method %>
Avoid unused expressions in silent ERB tags. `<% some_method %>` is evaluated but its return value is discarded. Use `<%= some_method %>` to output the value or remove the expression. (erb-no-unused-expressions)
erb
<% helper_call(arg) %>
Avoid unused expressions in silent ERB tags. `<% helper_call(arg) %>` is evaluated but its return value is discarded. Use `<%= helper_call(arg) %>` to output the value or remove the expression. (erb-no-unused-expressions)

References

-

Released under the MIT License.