renders_many
- Is there anyway to tell whether a single child or an array of children are to be rendered?
#1045
Answered
by
boardfish
brandoncordell
asked this question in
Q&A
-
Given that I have a basic component that looks like this class DropdownComponent < ViewComponent::Base
renders_many :items, lambda { |name: nil|
ItemComponent.new(name: name)
}
end I can use either of the following <%= render DropdownComponent.new do |c| %>
# I can render a single child
<% c.item(name: 'single child') %>
# or I can render a collection of children
<% c.items([{ name: 'child one' }, { name: 'child two' }]) %>
<% end %> I want to wrap
Is there anyway to do this? Is there a better way to architect this that I'm not seeing? |
Beta Was this translation helpful? Give feedback.
Answered by
boardfish
Aug 21, 2021
Replies: 1 comment 6 replies
-
Haven't tested this, but I think it might work. In your component template: <% if items.one? %>
<%= items.compact.first %>
<% else %>
<% items.each do |item| %>
<div><%= item %></div>
<% end %>
<% end %> |
Beta Was this translation helpful? Give feedback.
6 replies
Answer selected by
boardfish
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Haven't tested this, but I think it might work.
In your component template: