Skip to content

Linter Rule: Disallow module definitions in ERB templates

Rule:erb-no-module-definitions

Description

Disallow defining Ruby module structures and calling Module.new inside ERB templates. Like class definitions, modules introduce architectural logic that belongs in helper files, libraries, or initializers, not in view templates.

Rationale

Defining a module in an ERB file:

  • Violates separation of concerns
  • Pollutes the global or view context
  • Cannot be autoloaded, extended, or reused properly
  • Makes templates harder to read and debug
  • Can introduce unpredictable state when templates are re-rendered

Move modules to their appropriate locations in app/helpers/, app/, lib/, or other structural Ruby files.

Examples

✅ Good

ruby
# app/helpers/display_helpers.rb

module DisplayHelpers
  def item_count(count)
    pluralize(count, "item")
  end
end
erb
<%= item_count(2) %>

🚫 Bad

erb
<%
  module DisplayHelpers
Avoid defining modules in ERB templates. Move the module to a helper, library, or another appropriate Ruby file. (erb-no-module-definitions)
def item_count(count)
Avoid defining methods in ERB templates. Move `item_count` to a helper, presenter, or view component. (erb-no-method-definitions)
pluralize(count, "item") end end %> <%= item_count(2) %>
erb
<%
  decorator = Module.new do
Local variable `decorator` is assigned but never used. Remove the assignment, or prefix it with an underscore as `_decorator` to show it is intentionally unused. (erb-no-unused-local-variable)
Avoid defining modules in ERB templates. Move the module to a helper, library, or another appropriate Ruby file. (erb-no-module-definitions)
def item_count(count)
Avoid defining methods in ERB templates. Move `item_count` to a helper, presenter, or view component. (erb-no-method-definitions)
pluralize(count, "item") end end %>

Released under the MIT License.