Method: Hanami::Helpers::FormHelper::FormBuilder#fields_for_collection

Defined in:
lib/hanami/helpers/form_helper/form_builder.rb

#fields_for_collection(name) {|index, value| ... } ⇒ Object

Nested collections

Supports nesting for collections, with infinite levels of nesting.

Examples:

Basic usage

<%=
  form_for :delivery, routes.deliveries_path do
    text_field :customer_name

    fields_for_collection :addresses do
      text_field :street
    end

    submit 'Create'
  end
%>

<!-- output -->
<form action="/deliveries" method="POST" accept-charset="utf-8" id="delivery-form">
  <input type="hidden" name="_csrf_token" value="920cd5bfaecc6e58368950e790f2f7b4e5561eeeab230aa1b7de1b1f40ea7d5d">
  <input type="text" name="delivery[customer_name]" id="delivery-customer-name" value="">
  <input type="text" name="delivery[addresses][][street]" id="delivery-address-0-street" value="">
  <input type="text" name="delivery[addresses][][street]" id="delivery-address-1-street" value="">

  <button type="submit">Create</button>
</form>

Yield index and value

<%=
  form_for(:bill, routes.bill_path(id: bill.id), { bill: bill }, method: :patch, class: 'form-horizontal') do
    fieldset do
      legend "Addresses"

      fields_for_collection :addresses do |i, address|
        div class: "form-group" do
          text "Address id: #{address.id}"

          label :street
          input_text :street, class: "form-control", placeholder: "Street", "data-funky": "index-#{i}"
        end
      end

      label :ensure_names
    end

    submit submit_label, class: "btn btn-default"
  end
%>

<!-- output -->
<form action="/bills/1" method="POST" accept-charset="utf-8" id="bill-form" class="form-horizontal">
  <input type="hidden" name="_method" value="PATCH">
  <input type="hidden" name="_csrf_token" value="920cd5bfaecc6e58368950e790f2f7b4e5561eeeab230aa1b7de1b1f40ea7d5d">
  <fieldset>
    <legend>Addresses</legend>

    <div class="form-group">
      Address id: 23
      <label for="bill-addresses-0-street">Street</label>
      <input type="text" name="bill[addresses][][street]" id="bill-addresses-0-street" value="5th Ave" class="form-control" placeholder="Street" data-funky="index-0">
    </div>

    <div class="form-group">
      Address id: 42
      <label for="bill-addresses-1-street">Street</label>
      <input type="text" name="bill[addresses][][street]" id="bill-addresses-1-street" value="4th Ave" class="form-control" placeholder="Street" data-funky="index-1">
    </div>

    <label for="bill-ensure-names">Ensure names</label>
  </fieldset>

  <button type="submit" class="btn btn-default">Update</button>
</form>

Parameters:

  • name (Symbol)

    the nested name, it’s used to generate input names, ids, and to lookup params to fill values.

Yields:

  • (index, value)

Yield Parameters:

  • index (Integer)

    iterative index (it starts from zero)

  • value (Object)

    an item of the collection

Since:

  • 0.2.0



296
297
298
299
300
301
302
303
304
305
306
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 296

def fields_for_collection(name, &block)
  current_name = @name
  base_value = _value(name)
  @name = _input_name(name)

  base_value.each_with_index do |value, index|
    fields_for(index, value, &block)
  end
ensure
  @name = current_name
end