ViewComponent/view_component · error · ArgumentError

wrong number of arguments (given #{args.size}, expected 1)

Error message

wrong number of arguments (given #{args.size}, expected 1)

What it means

Inline template DSL methods (erb_template, haml_template, slim_template, ...) are captured by method_missing in ViewComponent::InlineTemplate and accept exactly one argument: the template source string. Passing zero arguments or more than one raises ArgumentError ('wrong number of arguments (given N, expected 1)') before any template is stored. Note the arity check runs after the duplicate-template check, so a clean class with a bad call fails here.

Source

Thrown at lib/view_component/inline_template.rb:18

# frozen_string_literal: true

module ViewComponent # :nodoc:
  module InlineTemplate
    extend ActiveSupport::Concern

    Template = Struct.new(:source, :language, :path, :lineno)

    class_methods do
      def method_missing(method, *args)
        return super if !method.end_with?("_template")

        if defined?(@__vc_inline_template_defined) && @__vc_inline_template_defined
          raise MultipleInlineTemplatesError
        end

        if args.size != 1
          raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 1)"
        end

        ext = method.to_s.gsub("_template", "")
        template = args.first

        @__vc_inline_template_language = ext

        caller = caller_locations(1..1)[0]
        @__vc_inline_template = Template.new(
          template,
          ext,
          caller.absolute_path || caller.path,
          caller.lineno
        )

        @__vc_inline_template_defined = true
      end

View on GitHub (pinned to 9f22c36fa7)

Solutions

  1. Pass exactly one String containing the full template source: erb_template <<-ERB ... ERB.
  2. If you need per-call options, encode them in the method suffix (haml_template/slim_template selects the language) rather than extra arguments.
  3. If the template is long or needs options, move it to a template file under app/components/<name>/ and drop the inline call.

Example fix

# before
class BadgeComponent < ViewComponent::Base
  erb_template # missing source
end

# after
class BadgeComponent < ViewComponent::Base
  erb_template <<-ERB
    <span class="badge"><%= content %></span>
  ERB
end
Defensive patterns

Strategy: validation

Validate before calling

source = "<div>...</div>"
raise ArgumentError, "template source must be a single String" unless source.is_a?(String)

CardComponent.erb_template(source)

Prevention

When it happens

Trigger: Calling erb_template with no source (forgot the heredoc); passing multiple strings like erb_template "<div>", "</div>"; passing options as a second positional argument (erb_template source, format: :html) instead of putting everything in the one string.

Common situations: Refactoring a heredoc and accidentally deleting the argument; developers assuming the DSL takes keyword options like template handlers do; copy-paste from Rails renderer docs where render takes multiple args.

Related errors


AI-assisted analysis of ViewComponent/view_component@9f22c36fa7 (2026-08-23). Data as JSON: /api/errors/0ca656414254b1fd. Report an issue: GitHub.