Skip to content

Linter Rule: Disallow method definitions inside ERB templates

Rule: erb-no-method-definitions

Description

Disallow defining Ruby methods inside ERB files using def ... end or define_method. View templates should only contain logic necessary for rendering the page. Defining methods in ERB mixes concerns and breaks MVC conventions.

Rationale

Defining methods in a view template:

  • Pollutes the view context
  • Makes templates harder to read and debug
  • Introduces global-like behavior into views
  • Encourages non-reusable, non-testable logic
  • Violates Rails' separation of concerns

Method definitions belong in helpers, presenters, or view components, not in view files.

Examples

✅ Good

ruby
# app/helpers/application_helper.rb

module ApplicationHelper
  def format_date(date)
    date.strftime("%B %d")
  end

  def admin?
    current_user.admin?
  end
end

🚫 Bad

erb
<% def format_date(date) %>
Avoid defining methods in ERB templates. Move `format_date` to a helper, presenter, or view component. (erb-no-method-definitions)
<%= date.strftime("%B %d") %> <% end %>
`<% end %>` appears outside its control flow block. Keep ERB control flow statements together within the same HTML scope (tag, attribute, or content). (`ERB_CONTROL_FLOW_SCOPE_ERROR`) (parser-no-errors)
<p><%= format_date(Date.today) %></p>
erb
<% def admin? = current_user.admin? %>
Avoid defining methods in ERB templates. Move `admin?` to a helper, presenter, or view component. (erb-no-method-definitions)
erb
<% define_method(:admin?) { current_user.admin? } %>
Avoid defining methods in ERB templates. Move this `define_method` call to a helper, presenter, or view component. (erb-no-method-definitions)

Released under the MIT License.