ViewComponent/view_component · error · ViewComponent::InvalidCollectionArgumentError
The value of the first argument passed to `with_collection`
Error message
The value of the first argument passed to `with_collection` isn't a valid collection. Make sure it responds to `to_ary`.
What it means
Error "The value of the first argument passed to `with_collection` isn't a valid collection. Make sure it responds to `to_ary`." thrown in ViewComponent/view_component.
Source
Thrown at lib/view_component/collection.rb:61
@collection.map do |item|
component.new(**component_options(item, iterator)).tap do |_|
iterator.iterate!
end
end
end
def initialize(component, object, spacer_component, **options)
@component = component
@collection = collection_variable(object || [])
@spacer_component = spacer_component
@options = options
end
def collection_variable(object)
if object.respond_to?(:to_ary)
object.to_ary
else
raise InvalidCollectionArgumentError
end
end
def component_options(item, iterator)
item_options = @options.dup
item_options[component.__vc_collection_parameter] = item
item_options[component.__vc_collection_counter_parameter] = iterator.index if component.__vc_counter_argument_present?
item_options[component.__vc_collection_iteration_parameter] = iterator.dup if component.__vc_iteration_argument_present?
item_options
end
# Render the spacer through a fresh `dup` so a collection rendered multiple
# times always gets a clean spacer instance.
def rendered_spacer(view_context)
return "" unless @spacer_component
@spacer_component.dup.render_in(view_context)View on GitHub (pinned to 9f22c36fa7)
Solutions
- Pass an object that responds to `to_ary` (e.g. an Array or ActiveRecord::Relation) as the first argument to `with_collection`.
- If the object is a single item, wrap it: with_collection([item]). If it is a custom enumerable, define `to_ary` or convert it with `.to_a`.
When it happens
Trigger: Thrown at lib/view_component/collection.rb:61 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of ViewComponent/view_component@9f22c36fa7 (2026-08-23).
Data as JSON: /api/errors/7f2222dcf7c88e62.
Report an issue: GitHub.