Rails helpers to render ViewComponents with less noise

Posted . Visible to the public.

Rendering ViewComponents can get verbose, especially for components that are used frequently:

<%= render Components::Icon.new(name: :check, text: "Done") %>

For frequently used components, consider adding a Rails helper in app/helpers/components_helper.rb:

module ComponentsHelper
  def icon(...)
    render Components::Icon.new(...)
  end
end

Now your views can simply say:

<%= icon(name: :check, text: "Done") %>

The helper forwards all arguments to the component's constructor. Defaults and other configuration remain in the component constructor, so the helper needs no maintenance when its arguments change.

This works best for commonly used components. For rarely used components, maintaining an additional helper may not be worth it.

Generating helpers in bulk

If you have many components, you can generate helper methods instead of maintaining each one manually.

Two implementations to draw inspiration from:

Profile picture of Henning Koch
Henning Koch
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2026-11-03 10:47)