Linter Rule: Disallow instance variables in partials
Rule: erb-no-instance-variables-in-partials
Description
Prevent usage of instance variables inside ERB partials. Using instance variables inside partials can cause issues as their dependency is defined outside of the partial itself. This makes partials more fragile and less reusable. Local variables should be passed directly to partial renders.
A partial is any template whose filename begins with an underscore (e.g. _card.html.erb).
Instance variables in partials create implicit dependencies on the controller or parent view, making partials harder to reuse, test, and reason about. Passing data as local variables makes the partial's interface explicit and self-documenting.
Examples
✅ Good
erb
<%= render partial: "posts/card", locals: { post: @post } %>erb
<div>
<%= post.title %>
</div>🚫 Bad
erb
<%= render partial: "posts/card" %>erb
<div>
<%= @post.title %>
</div>