Nested Form

Add and remove rows of nested fields for accepts_nested_attributes_for.

Usage

<%# class Trip: has_many :stops; accepts_nested_attributes_for :stops, allow_destroy: true %>
<%= form_with model: @trip do |f| %>
  <%= ui.nested_form do %>
    <%= ui.nested_form_template do %>
      <%= f.fields_for :stops, Stop.new, child_index: "NEW_RECORD" do |stop| %>
        <%= render "stop_fields", form: stop %>
      <% end %>
    <% end %>

    <%= f.fields_for :stops do |stop| %>
      <%= render "stop_fields", form: stop %>
    <% end %>

    <%= ui.nested_form_add "Add stop" %>
  <% end %>
<% end %>

<%# _stop_fields.html.erb %>
<%= ui.nested_form_item new_record: form.object.new_record? do %>
  <%= form.text_field :city %>
  <%= form.hidden_field :_destroy %>
  <%= ui.nested_form_remove %>
<% end %>

<%# TripsController: params.expect(trip: [:name, stops_attributes: [[:id, :city, :_destroy]]]) %>
Props
PropTypeDefaultDescription
minInteger0Rows that cannot be removed. At the minimum the remove buttons are hidden.
maxIntegernilMost rows allowed. At the maximum the add button is hidden.
use_discardBooleanfalseFlag removed saved rows with a `_discard` field instead of `_destroy`, for soft deletion.
insert_positionString
"beforebegin" | "afterend"
"beforebegin"Where a new row goes relative to the add button: before it, or after it for an add button above the rows and the newest row first.
data-nested-form-target="index"Target—Shows the row's number, 1, 2, 3…, renumbered as rows come and go.
data-nested-form-target="position"Target—An input that gets the row's number, so the form saves the order. Works together with Sortable's position target.
form__nested_contentClass—Wrap a row's fields in an element with this class, and removing a saved row drops them, keeping only the id and `_destroy`. A required field in a removed row then cannot block the form.
data-action="blur->nested-form#removeAssociationIfEmpty"Action—Removes the row when the field is left empty.
nested-form:added / nested-form:removedEvent—Dispatched on the container after a row is added or removed, with the row as `detail.item`.

Also accepts any HTML attributes via **options (e.g., id:, data:, aria:). class: is also supported for custom styling.

SubcomponentsUse subcomponents below or any HTML.
NameHelperDescription
nested_form_templateui.nested_form_templateThe fields of a new row, built with `fields_for … child_index: "NEW_RECORD"`. It is never submitted; every added row gets its own index in place of NEW_RECORD.
nested_form_itemui.nested_form_itemA row. Saved rows also need the `_destroy` hidden field. Accepts any HTML attributes.
nested_form_addui.nested_form_addThe add button. New rows are inserted next to it.
nested_form_removeui.nested_form_removeThe remove button of a row, a trash icon unless given content.

Examples

Minimum and Maximum

<% trip = Docs::Trip.new(stops: [Docs::Stop.new(id: 1, city: "Berlin")]) %>
<%= form_with model: trip, scope: :weekend, url: root_path do |f| %>
  <%= ui.nested_form min: 1, max: 3 do %>
    <%= ui.nested_form_template do %>
      <%= f.fields_for :stops, Docs::Stop.new, child_index: "NEW_RECORD" do |stop| %>
        <%= ui.nested_form_item new_record: true do %>
          <%= stop.text_field :city, label: false, placeholder: "City" %>
          <%= ui.nested_form_remove %>
        <% end %>
      <% end %>
    <% end %>

    <%= f.fields_for :stops do |stop| %>
      <%= ui.nested_form_item do %>
        <%= stop.text_field :city, label: false, placeholder: "City" %>
        <%= stop.hidden_field :_destroy %>
        <%= ui.nested_form_remove %>
      <% end %>
    <% end %>

    <%= ui.nested_form_add "Add stop" %>
  <% end %>
<% end %>

Numbered Rows

<%= form_with model: Docs::Trip.new, scope: :road_trip, url: root_path do |f| %>
  <%= ui.nested_form do %>
    <%= ui.nested_form_template do %>
      <%= f.fields_for :stops, Docs::Stop.new, child_index: "NEW_RECORD" do |stop| %>
        <%= ui.nested_form_item new_record: true, class: "items-center" do %>
          <span class="w-14 text-sm text-muted-foreground">Stop <span data-nested-form-target="index"></span></span>
          <%= stop.text_field :city, label: false, placeholder: "City" %>
          <%= ui.nested_form_remove %>
        <% end %>
      <% end %>
    <% end %>

    <%= ui.nested_form_add "Add stop" %>
  <% end %>
<% end %>

Confirm Before Removing

<% trip = Docs::Trip.new(stops: [Docs::Stop.new(id: 1, city: "Kyoto"), Docs::Stop.new(id: 2, city: "Osaka")]) %>
<%= form_with model: trip, scope: :japan, url: root_path do |f| %>
  <%= ui.nested_form do %>
    <%= ui.nested_form_template do %>
      <%= f.fields_for :stops, Docs::Stop.new, child_index: "NEW_RECORD" do |stop| %>
        <%= ui.nested_form_item new_record: true do %>
          <%= stop.text_field :city, label: false, placeholder: "City" %>
          <%= ui.nested_form_remove %>
        <% end %>
      <% end %>
    <% end %>

    <%= f.fields_for :stops do |stop| %>
      <%= ui.nested_form_item do %>
        <%= stop.text_field :city, label: false, placeholder: "City" %>
        <%= stop.hidden_field :_destroy %>
        <%= ui.nested_form_remove confirm: "Remove #{stop.object.city} from the trip?" %>
      <% end %>
    <% end %>

    <%= ui.nested_form_add "Add stop" %>
  <% end %>
<% end %>

Several Fields in a Row

<% trip = Docs::Trip.new(stops: [Docs::Stop.new(id: 1, city: "Reykjavík", nights: 2), Docs::Stop.new(id: 2, city: "Vík", nights: 1)]) %>
<%= form_with model: trip, scope: :iceland, url: root_path do |f| %>
  <%= ui.nested_form do %>
    <%= ui.nested_form_template do %>
      <%= f.fields_for :stops, Docs::Stop.new, child_index: "NEW_RECORD" do |stop| %>
        <%= ui.nested_form_item new_record: true do %>
          <div class="form__nested_content">
            <%= stop.text_field :city, required: true %>
            <%= stop.number_field :nights, min: 1, group_html: { class: "flex-none w-24" } %>
          </div>
          <%= ui.nested_form_remove %>
        <% end %>
      <% end %>
    <% end %>

    <%= f.fields_for :stops do |stop| %>
      <%= ui.nested_form_item do %>
        <div class="form__nested_content">
          <%= stop.text_field :city, required: true %>
          <%= stop.number_field :nights, min: 1, group_html: { class: "flex-none w-24" } %>
        </div>
        <%= stop.hidden_field :_destroy %>
        <%= ui.nested_form_remove %>
      <% end %>
    <% end %>

    <%= ui.nested_form_add "Add stop" %>
  <% end %>
<% end %>

Choices in Each Row

<% trip = Docs::Trip.new(stops: [Docs::Stop.new(id: 1, city: "Paris")]) %>
<% cities = %w[Amsterdam Barcelona Berlin Lisbon Paris Prague Rome Vienna] %>
<%= form_with model: trip, scope: :interrail, url: root_path do |f| %>
  <%= ui.nested_form do %>
    <%= ui.nested_form_template do %>
      <%= f.fields_for :stops, Docs::Stop.new, child_index: "NEW_RECORD" do |stop| %>
        <%= ui.nested_form_item new_record: true do %>
          <%= stop.choices :city, cities, { include_blank: "Select a city…" }, { label: false } %>
          <%= ui.nested_form_remove %>
        <% end %>
      <% end %>
    <% end %>

    <%= f.fields_for :stops do |stop| %>
      <%= ui.nested_form_item do %>
        <%= stop.choices :city, cities, { include_blank: "Select a city…" }, { label: false } %>
        <%= stop.hidden_field :_destroy %>
        <%= ui.nested_form_remove %>
      <% end %>
    <% end %>

    <%= ui.nested_form_add "Add stop" %>
  <% end %>
<% end %>

Newest Row First

<%= form_with model: Docs::Trip.new, scope: :wishlist, url: root_path do |f| %>
  <%= ui.nested_form insert_position: "afterend" do %>
    <%= ui.nested_form_template do %>
      <%= f.fields_for :stops, Docs::Stop.new, child_index: "NEW_RECORD" do |stop| %>
        <%= ui.nested_form_item new_record: true do %>
          <%= stop.text_field :city, label: false, placeholder: "A city you want to see" %>
          <%= ui.nested_form_remove %>
        <% end %>
      <% end %>
    <% end %>

    <%= ui.nested_form_add "Add city" %>
  <% end %>
<% end %>

Discard Instead of Destroy

<% trip = Docs::Trip.new(stops: [Docs::Stop.new(id: 1, city: "Prague"), Docs::Stop.new(id: 2, city: "Vienna")]) %>
<%= form_with model: trip, scope: :central_europe, url: root_path do |f| %>
  <%= ui.nested_form use_discard: true do %>
    <%= ui.nested_form_template do %>
      <%= f.fields_for :stops, Docs::Stop.new, child_index: "NEW_RECORD" do |stop| %>
        <%= ui.nested_form_item new_record: true do %>
          <%= stop.text_field :city, label: false, placeholder: "City" %>
          <%= ui.nested_form_remove %>
        <% end %>
      <% end %>
    <% end %>

    <%= f.fields_for :stops do |stop| %>
      <%= ui.nested_form_item do %>
        <%= stop.text_field :city, label: false, placeholder: "City" %>
        <%= stop.hidden_field :_discard %>
        <%= ui.nested_form_remove %>
      <% end %>
    <% end %>

    <%= ui.nested_form_add "Add stop" %>
  <% end %>
<% end %>

Sortable Rows

<% trip = Docs::Trip.new(stops: [Docs::Stop.new(id: 1, city: "Rome", position: 1), Docs::Stop.new(id: 2, city: "Florence", position: 2), Docs::Stop.new(id: 3, city: "Venice", position: 3)]) %>
<%= form_with model: trip, scope: :italy, url: root_path do |f| %>
  <%= ui.nested_form do %>
    <%= ui.nested_form_template do %>
      <%= f.fields_for :stops, Docs::Stop.new, child_index: "NEW_RECORD" do |stop| %>
        <%= ui.sortable_item as: :nested_form_item, new_record: true, class: "items-center" do %>
          <%= ui.sortable_handle %>
          <%= stop.number_field :position, label: false, readonly: true, group_html: { class: "flex-none w-16" }, data: { sortable_target: "position", nested_form_target: "position" } %>
          <%= stop.text_field :city, label: false, placeholder: "City" %>
          <%= ui.nested_form_remove %>
        <% end %>
      <% end %>
    <% end %>

    <%= ui.sortable class: "flex flex-col gap-3" do %>
      <%= f.fields_for :stops do |stop| %>
        <%= ui.sortable_item as: :nested_form_item, class: "items-center" do %>
          <%= ui.sortable_handle %>
          <%= stop.number_field :position, label: false, readonly: true, group_html: { class: "flex-none w-16" }, data: { sortable_target: "position", nested_form_target: "position" } %>
          <%= stop.text_field :city, label: false, placeholder: "City" %>
          <%= stop.hidden_field :_destroy %>
          <%= ui.nested_form_remove %>
        <% end %>
      <% end %>

      <%= ui.nested_form_add "Add stop" %>
    <% end %>
  <% end %>
<% end %>

Are you absolutely sure?

This cannot be undone.