skip to content

In Rails, what do render @gigs and render partial: "gig", collection: do, and how do as:, spacer_template:, gig_counter and gig_iteration work?

level: middleimportance: must knowfreq 55%

answer

  1. one partial per element
  2. empty collection returns nil
  3. as: renames the local and counter
  4. counter starts at 0
  5. iteration: index, size, first?, last?

basics

~20 s

Rails' render @gigs, or render partial: "gig", collection: @gigs, renders the partial once per element, exposing gig, gig_counter from 0 and gig_iteration; as: renames them, spacer_template: renders between items, and an empty collection returns nil.

solid answer

~40 s

Collection rendering runs one partial per element in a single `render` call. `render @gigs` derives the partial from each element's `to_partial_path`, so a mixed collection can render different partials; `render partial: "gig", collection: @gigs` names it. Each element arrives as a local named after the partial, `gig`, or the name given with `as:`. Rails also sets `gig_counter`, a zero-based index, and `gig_iteration`, an object with `index`, `size`, `first?` and `last?`; with `as: :show` they become `show_counter` and `show_iteration`. `locals:` are shared by every element. `spacer_template: "gig_divider"` renders a partial between items. An empty collection returns `nil`, so `render(@gigs) || "No gigs announced yet"` gives an empty state.

code

erb · 13 lines
erb
<%# app/views/gigs/index.html.erb %>
<section class="gig-list">
  <%= render(partial: "gig", collection: @gigs,
             locals: { show_venue: true },
             spacer_template: "gig_divider") || tag.p("No gigs announced yet.") %>
</section>

<%# app/views/gigs/_gig.html.erb %>
<article class="gig<%= " gig--featured" if gig_iteration.first? %>">
  <span class="gig__position"><%= gig_counter + 1 %>.</span>
  <h2><%= gig.band_name %></h2>
  <% if show_venue %><p><%= gig.venue.name %></p><% end %>
</article>

go deeper

for a junior

Know that render @gigs renders the gig partial once per record and that the element arrives as a local named after the partial.

for a middle

Explain counters and iteration objects, as:, shared locals:, spacer_template: and the nil return for empty collections.

for a senior

Use collection rendering by default on list pages and keep per-card data loaded before rendering so the list stays fast.

for a principal

Standardise list rendering patterns, empty states and card partials so teams do not each invent their own loop.

## Collection rendering in one call A concert-listings page shows a list of gig cards. Writing an `each` loop with a `render` inside works, but Action View has a dedicated form that renders a partial once per element: ```erb <%= render partial: "gig", collection: @gigs %> <%= render @gigs %> <%# same, partial derived from the records %> ``` Both return the joined HTML of every rendered card as one html-safe string. ## What each card receives | Local | Value | With `as: :show` | |---|---|---| | `gig` | the current element | `show` | | `gig_counter` | 0, 1, 2, ... | `show_counter` | | `gig_iteration` | an `ActionView::PartialIteration` | `show_iteration` | | every key in `locals:` | the same value for all elements | unchanged | `PartialIteration` offers `index` (same as the counter), `size` (the collection's size), `first?` and `last?`, which is the clean way to add a class to the first card or skip a separator after the last one. ## The options - **`collection:`** the elements to render. With `render partial:`, a `nil` collection is treated as empty. - **`as:`** the local's name, when the partial name does not suit, for example `render partial: "listing", collection: @gigs, as: :gig`. It must be a valid Ruby identifier. - **`locals:`** extra values shared by every element, for example `locals: { show_venue: false }`. - **`spacer_template:`** a partial rendered between consecutive elements, never before the first or after the last; it receives the shared `locals:`, not the element. - **`layout:`** a partial layout wrapped around each element. ## `render @gigs` and mixed collections The shorthand asks each element for `to_partial_path`. For a relation of `Gig` records that is `gigs/gig` for all of them, so Rails finds the template once. For a mixed array, say gigs and festival announcements on one timeline, each element can map to a different partial, `gigs/_gig` and `festivals/_festival`, and Rails picks the right one per element. The counter then counts positions in the whole list, not per model. ## Empty collections When there is nothing to render, collection rendering returns `nil` rather than an empty string. That makes a fallback a one-liner: ```erb <%= render(@gigs) || tag.p("No gigs announced yet.", class: "empty") %> ``` The parentheses matter; without them `||` would bind to the argument. ## Practical guidance 1. Prefer `render @gigs` when the records map to their conventional partial; use the explicit `partial:` form when the partial is named differently or lives elsewhere. 2. Use `gig_iteration.last?` rather than comparing `gig_counter` to a size you computed separately. 3. Keep shared values in `locals:` instead of instance variables, so the partial stays reusable. 4. Load what the cards display before rendering; collection rendering does not add eager loading on its own, so a card that reads `gig.venue` still needs the relation to preload venues. ## Mistakes worth spotting - `render partial: "listing", collection: @gigs` without `as:` names the local `listing`, not `gig`, because the local follows the partial's name. - `@gigs.each_with_index` around a `render` call recreates `gig_counter` by hand and loses the single collection render. - Computing "is this the last card?" with `gig_counter == @gigs.size` is off by one; `gig_iteration.last?` is exact. - Putting per-card logic in the spacer template: it is rendered once, without the element, and reused between every pair. ## Logs In development the log shows one line for the whole collection, such as `Rendered collection of gigs/_gig.html.erb [48 times]`, instead of 48 `Rendered gigs/_gig.html.erb` lines, which is also a quick way to confirm which form a page uses.

  • What changes inside the partial when the call adds `as: :listing`?
    The element arrives as `listing` instead of the partial's name, and the counter and iteration locals follow it: `listing_counter` and `listing_iteration`. Code in the partial that still says `gig` or `gig_counter` would fail.
  • Does the spacer template see the gig it sits next to?
    No. The spacer is rendered once with the call's shared `locals:` and its output is inserted between elements, so it cannot depend on the neighbouring records. If a divider must know its neighbours, render it from the main partial using `gig_iteration`.

  • gig_counter starts at 1 for the first element.
  • An empty collection renders an empty string, so || never applies.
  • as: renames the element but the counter stays gig_counter.
  • render @gigs only works when every element is the same model.
  • spacer_template: is rendered once per element, receiving that element.