In ViewComponent, how do renders_one and renders_many give a card component header and action regions, and how does a caller fill them?
answer
- named regions besides content
- getter, with_ setter, predicate
- renders_many setter is singular
- slot can be a class or lambda
- with_ calls go in non-printing tags
basics
~20 srenders_one :header and renders_many :actions declare named regions. The caller fills them inside the render block with card.with_header { ... } and card.with_action(...), and the template prints header, iterates actions, checks header? and prints content for the body.
solid answer
~40 s`renders_one :header` defines a getter `header`, a setter `with_header` and a predicate `header?`; `renders_many :actions` defines `actions` (an array, empty when unset), the singular setter `with_action`, the plural `with_actions(collection)` and `actions?`. A slot with no second argument just captures the block the caller passes; with a component class (`renders_many :actions, ClaimActionComponent`) the setter's arguments go to that class's `new`; with a lambda the lambda builds a component or returns markup, and polymorphic slots use `types:`. The caller writes the `with_*` calls in `<% %>` tags inside the block given to `render`; whatever else the block outputs becomes `content`, the body. The setters return the slot, so `<%= card.with_header %>` would also print it into the body. Calling `with_header` twice keeps the last one, and a public `default_header` method supplies a fallback.
code
ruby · 14 lines# app/components/claim_card_component.rb
class ClaimCardComponent < ViewComponent::Base
renders_one :header
renders_many :actions, ClaimActionComponent
def initialize(claim:)
@claim = claim
end
# must be public: ViewComponent looks it up among public instance methods
def default_header
"Claim #{@claim.number}"
end
endgo deeper
Recall renders_one and renders_many, the with_ setters, and that the render block's other output becomes content.
Explain the generated getter, setter and predicate, passthrough versus class versus lambda slots, defaults, and why setters go in non-printing tags.
Design slot APIs that keep layout inside the component, choose inputs over slots for data, and migrate pre-v3 setter calls.
Decide how far a design system's components expose regions versus fixed layouts, trading flexibility for consistency across teams.
## Regions beyond one block of content A claims portal renders every claim in a **card**: a header with the claim number and status, a body with the summary, and a footer row of action buttons (Approve, Request documents). A single `content` block cannot say which markup goes where. **Slots** give a component named regions that callers fill, while the component keeps control of the surrounding layout. ## Declaring slots ```ruby class ClaimCardComponent < ViewComponent::Base renders_one :header renders_many :actions, ClaimActionComponent def initialize(claim:) @claim = claim end end ``` Each declaration generates methods: | Declaration | Getter | Setter(s) | Predicate | |---|---|---|---| | `renders_one :header` | `header` (a slot or `nil`) | `with_header`, `with_header_content("...")` | `header?` | | `renders_many :actions` | `actions` (an array, `[]` when empty) | `with_action`, `with_action_content`, `with_actions(list)` | `actions?` | The second argument decides what a slot holds: 1. **Nothing**: the slot is a **passthrough** that captures the caller's block. 2. **A component class** (or its name as a string): the setter's arguments are passed to that class's `new`, and the caller's block becomes that component's `content`. 3. **A lambda**: it runs in the parent component's context and returns either a component or markup, useful for wrapping (`-> (label:) { tag.h2(label) }`). 4. **`types: { ... }`**: a **polymorphic** slot; each type gets its own setter such as `with_visual_icon` or `with_visual_avatar`. The names `content` and `render` (and their plurals) are reserved and raise when used as slot names. ## The template ```erb <article class="claim-card"> <% if header? %> <header><%= header %></header> <% end %> <div class="claim-card__body"><%= content %></div> <% if actions? %> <footer><% actions.each do |action| %><%= action %><% end %></footer> <% end %> </article> ``` Reading any slot first evaluates the block the caller passed to `render`, so the `with_*` calls have run before the template asks for `header`. ## Filling them from a view ```erb <%= render(ClaimCardComponent.new(claim: @claim)) do |card| %> <% card.with_header do %>Claim <%= @claim.number %><% end %> <% card.with_action(label: "Approve", url: approve_claim_path(@claim)) %> <% card.with_action(label: "Request documents", url: claim_requests_path(@claim)) %> <p><%= @claim.summary %></p> <% end %> ``` Rules that keep it correct: - The `with_*` calls sit in **`<% %>`** tags. Each setter returns the slot object, which renders when printed, so `<%= card.with_header ... %>` would push the header into the block's output and therefore into `content`, the body. - Everything the block prints apart from those calls becomes **`content`**. - A `renders_one` slot set twice keeps the **last** value; a `renders_many` setter **appends**. - A public **`default_header`** method on the component supplies the header when the caller sets none; ViewComponent finds it among public instance methods, so a private one is ignored. - Slots are not a substitute for inputs: data the component needs to compute something (the claim, a status) belongs in `initialize`; slots are for markup the caller chooses. ## Since ViewComponent 3 Older code called `card.header(...)` to **set** a slot. ViewComponent 3 removed those setters: the plain name is now only the getter, and every setter starts with `with_`. Code that still calls the old form reaches the getter instead: arguments raise `ArgumentError`, and a call with only a block sets nothing. ## Mistakes interviewers listen for - **Old setter syntax.** `card.header(...)` is the getter; setting is always `with_header`. - **Plural setter for one item.** `with_actions` takes a list of argument sets; one action is `with_action`. - **Printing setters.** `<%= card.with_header ... %>` puts the header into the body as well. - **Expecting nil from an empty collection slot.** `actions` is `[]`, so `.each` is safe; use `actions?` to skip wrapper markup. - **Private defaults.** A `default_header` defined under `private` is not picked up. - **Data through slots.** Passing the claim's status as slot markup instead of an `initialize` input hides it from the component's own logic and tests.
- What does renders_many :actions return in the template when the caller set no actions?An empty array, so `actions.each` simply renders nothing; `actions?` returns false, which lets the template skip the surrounding footer element. A `renders_one` getter returns nil when unset, unless the component defines a `default_header`-style fallback.
- When would you use a lambda slot instead of a component class?When the slot only needs light wrapping or argument mapping, for example `renders_one :title, -> (text:) { tag.h2(text, class: "card-title") }`. The lambda runs in the parent component, so it can use its helpers and state; a separate class is better once the region has its own logic or tests.
Slots are the labelled boxes on a printed claim form: the form decides where the header box and the signature boxes sit, and whoever fills it in only decides what goes into each box.
- Calling card.header(...) sets the header slot in ViewComponent 4
- A renders_many slot is set with with_actions for each single action
- <%= card.with_header { ... } %> is the right tag for setting a slot
- Setting a renders_one slot twice renders both headers
- An unset renders_many slot returns nil and breaks .each