Skip to content

Linter Rule: Disallow content arguments for void Action View elements

Rule: actionview-no-void-element-content

Description

Void HTML elements like img, br, and hr cannot have content. When using Action View helpers like tag.img or content_tag :img, passing a positional content argument will raise a runtime error in Rails.

Rationale

In Rails, tag.* helpers for void elements do not accept positional arguments for content. Calling tag.img "/image.png" raises wrong number of arguments (given 1, expected 0) at runtime.

The correct way to set attributes on void elements is to use keyword arguments, such as tag.img src: "/image.png". This rule catches the error at lint time before it reaches production.

Examples

Good

erb
<%= tag.img src: "/image.png", alt: "Photo" %>
erb
<%= tag.br %>
erb
<%= tag.hr class: "divider" %>
erb
<%= content_tag :img, src: "/image.png", alt: "Photo" %>

Bad

erb
<%= tag.img "/image.png" %>
Void element `img` cannot have content. `tag.img` does not accept a positional argument for content. (actionview-no-void-element-content)
erb
<%= tag.br "hello" %>
Void element `br` cannot have content. `tag.br` does not accept a positional argument for content. (actionview-no-void-element-content)
erb
<%= content_tag :img, "hello" %>
Void element `img` cannot have content. `content_tag :img` does not accept a positional argument for content. (actionview-no-void-element-content)
erb
<%= content_tag :br, "hello" %>
Void element `br` cannot have content. `content_tag :br` does not accept a positional argument for content. (actionview-no-void-element-content)

References

Released under the MIT License.