Skip to content

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

app/views/posts/index.html.erb
erb
<%= render partial: "posts/card", locals:post: @post } %>
app/views/posts/_card.html.erb
erb
<div>
  <%= post.title %>
</div>

🚫 Bad

app/views/posts/index.html.erb
erb
<%= render partial: "posts/card" %>
app/views/posts/_card.html.erb
erb
<div>
  <%= @post.title %>
</div>

References

Released under the MIT License.