Skip to content

Rule: erb-no-then-in-control-flow

Description

Disallow the use of the Ruby then keyword in control flow expressions inside ERB templates. This applies to:

  • if … then
  • elsif … then
  • unless … then
  • case … when … then
  • case … in … then

Rationale

While Ruby allows the then keyword, its use inside ERB templates significantly reduces readability and adds confusion/ambiguity.

In templates, clarity matters more than terseness. The multiline form:

  • Is easier to read and review
  • Works better with indentation and formatting rules
  • Avoids subtle parsing confusion in mixed HTML/Ruby contexts
  • Matches idiomatic Rails view style

This rule enforces a consistent, block-oriented style for control flow in ERB.

Examples

Good

erb
<% if condition %>
  yes
<% end %>
erb
<% unless logged_in? %>
  please log in
<% end %>
erb
<% case status %>
<% when :ok %>
  success
<% when :error %>
  failure
<% else %>
  unknown
<% end %>
erb
<% case value %>
<% in Integer %>
  number
<% in String %>
  string
<% end %>

Bad

erb
<% if condition then %>
Avoid using `then` in `if` expressions inside ERB templates. Use the multiline block form instead. (erb-no-then-in-control-flow)
yes <% end %>
erb
<% case status %>
<% when :ok then %>
Avoid using `then` in `when` expressions inside ERB templates. Use the multiline block form instead. (erb-no-then-in-control-flow)
success <% end %>
erb
<% case value %>
<% in Integer then "number" %>
Avoid using `then` in `in` expressions inside ERB templates. Use the multiline block form instead. (erb-no-then-in-control-flow)
<% in String then "text" %>
Avoid using `then` in `in` expressions inside ERB templates. Use the multiline block form instead. (erb-no-then-in-control-flow)
<% end %>

References

Released under the MIT License.