112

Is there a method (or way to pull off similar functionality) to do a fields_for_with_index?

Example:

<% f.fields_for_with_index :questions do |builder, index| %>  
  <%= render 'some_form', :f => builder, :i => index %>
<% end %>

That partial being rendered needs to know what the current index is in the fields_for loop.

1
  • 3
    I'd + one this in core rails... Keep coming across a case for this one too. Aug 5, 2011 at 14:43

9 Answers 9

164

The answer is quite simple as the solution is provided within Rails. You can use f.options params. So, inside your rendered _some_form.html.erb,

Index can be accessed by:

<%= f.options[:child_index] %>

You don't need to do anything else.


Update: It seems that my answer wasn't clear enough...

Original HTML File:

<!-- Main ERB File -->
<% f.fields_for :questions do |builder| %>  
  <%= render 'some_form', :f => builder %>
<% end %>

Rendered Sub-Form:

<!-- _some_form.html.erb -->
<%= f.options[:child_index] %>
6
  • 11
    it's not working here. can you provide rails documentation link? Sep 3, 2013 at 20:37
  • 2
    @LucasRenan & @graphmeter - Please read the question again, you need to call <%= f.options[:child_index] %> in your rendered sub-form (In this case: _some_form.html.erb), not int the original builder. Answer updated for more clarification.
    – Sheharyar
    Nov 8, 2013 at 21:44
  • 1
    Odd, I get nil for that
    – bcackerman
    Dec 14, 2014 at 19:07
  • This only works on Rails 3 if you've manually set :child_index
    – eltiare
    Apr 6, 2015 at 23:48
  • 1
    @Sheharyar This is working in Rails 4. But this is giving me values like '1450681048049,1450681050158,1450681056830,1450681141951,1450681219262'. But I need index in this form '1,2,3,4,5'. What should I do?
    – vidal
    Dec 21, 2015 at 11:32
123

As of Rails 4.0.2, an index is now included in the FormBuilder object:

https://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-fields_for

For example:

<%= form_for @person do |person_form| %>
  ...
  <%= person_form.fields_for :projects do |project_fields| %>
    Project #<%= project_fields.index %>
  ...
  <% end %>
  ...
<% end %>
3
  • 2
    This works and is less to write out than project_fields.options[:child_index]. So I like this way better!
    – HighHopes
    Apr 7, 2017 at 15:43
  • its working fine.. is there an option to use index +1 ? Because it is starting with ZERO Jul 27, 2022 at 21:44
  • 1
    This is the cleanest answer and uses rails functionality rather than a work-around.
    – Nick Res
    Sep 9, 2023 at 21:47
94

The answer below was posted many years ago, for a modern approach see: https://stackoverflow.com/a/22640703/105403


This would actually be a better approach, following Rails documentation more closely:

<% @questions.each.with_index do |question,index| %>
    <% f.fields_for :questions, question do |fq| %>  
        # here you have both the 'question' object and the current 'index'
    <% end %>
<% end %>

From: http://railsapi.com/doc/rails-v3.0.4/classes/ActionView/Helpers/FormHelper.html#M006456

It’s also possible to specify the instance to be used:

  <%= form_for @person do |person_form| %>
    ...
    <% @person.projects.each do |project| %>
      <% if project.active? %>
        <%= person_form.fields_for :projects, project do |project_fields| %>
          Name: <%= project_fields.text_field :name %>
        <% end %>
      <% end %>
    <% end %>
  <% end %>
6
  • 2
    I wish there was something built into fields_for for this, but since there isn't your answer saved my day. Thanks. Feb 1, 2012 at 9:27
  • 1
    You can also use the undocumented option :child_index on fields_for, if you need more control over which index is rendered, like this: fields_for(:projects, project, child_index: index) Feb 1, 2012 at 9:34
  • 10
    Rails 4.0.2+ users should check out Ben's answer as index is now built into the builder.
    – notapatch
    Oct 10, 2014 at 10:29
  • 1
    @Marco You are the king Sir. you saved my day to day. :-) Wish I could give you couple of 10+......!
    – user5084534
    Oct 7, 2016 at 12:04
  • 1
    This answer should be edit and update with Ben's answer as notapatch said Sep 25, 2020 at 14:37
18

For Rails 4+

<%= form_for @person do |person_form| %>
  <%= person_form.fields_for :projects do |project_fields| %>
    <%= project_fields.index %>
  <% end %>
<% end %>

Monkey Patch For Rails 3 Support

To get f.index to work in Rails 3 you need to add an monkey patch to your projects initializers to add this functionality to fields_for

# config/initializers/fields_for_index_patch.rb

module ActionView
  module Helpers
    class FormBuilder

      def index
        @options[:index] || @options[:child_index]
      end

      def fields_for(record_name, record_object = nil, fields_options = {}, &block)
        fields_options, record_object = record_object, nil if record_object.is_a?(Hash) && record_object.extractable_options?
        fields_options[:builder] ||= options[:builder]
        fields_options[:parent_builder] = self
        fields_options[:namespace] = options[:namespace]

        case record_name
          when String, Symbol
            if nested_attributes_association?(record_name)
              return fields_for_with_nested_attributes(record_name, record_object, fields_options, block)
            end
          else
            record_object = record_name.is_a?(Array) ? record_name.last : record_name
            record_name   = ActiveModel::Naming.param_key(record_object)
        end

        index = if options.has_key?(:index)
                  options[:index]
                elsif defined?(@auto_index)
                  self.object_name = @object_name.to_s.sub(/\[\]$/,"")
                  @auto_index
                end

        record_name = index ? "#{object_name}[#{index}][#{record_name}]" : "#{object_name}[#{record_name}]"
        fields_options[:child_index] = index

        @template.fields_for(record_name, record_object, fields_options, &block)
      end

      def fields_for_with_nested_attributes(association_name, association, options, block)
        name = "#{object_name}[#{association_name}_attributes]"
        association = convert_to_model(association)

        if association.respond_to?(:persisted?)
          association = [association] if @object.send(association_name).is_a?(Array)
        elsif !association.respond_to?(:to_ary)
          association = @object.send(association_name)
        end

        if association.respond_to?(:to_ary)
          explicit_child_index = options[:child_index]
          output = ActiveSupport::SafeBuffer.new
          association.each do |child|
            options[:child_index] = nested_child_index(name) unless explicit_child_index
            output << fields_for_nested_model("#{name}[#{options[:child_index]}]", child, options, block)
          end
          output
        elsif association
          fields_for_nested_model(name, association, options, block)
        end
      end

    end
  end
end
1
  • Better answer so far, use .index is much cleaner. Aug 17, 2018 at 13:17
7

Checkout Rendering a collection of partials. If your requirement is that a template needs to iterate over an array and render a sub template for each of the elements.

<%= f.fields_for @parent.children do |children_form| %>
  <%= render :partial => 'children', :collection => @parent.children, 
      :locals => { :f => children_form } %>
<% end %>

This will render “_children.erb“ and pass the local variable 'children' to the template for display. An iteration counter will automatically be made available to the template with a name of the form partial_name_counter. In the case of the example above, the template would be fed children_counter.

Hope this helps.

2
  • I get "undefined local variable or method 'question_comment_form_counter'" when doing that. question_comment_form being my partial name...
    – Shpigford
    Jan 31, 2011 at 17:23
  • Do question has_many :comments and how are you building comments for example in your new or edit action of the questions try doing 1.times { question.comments.build }
    – Syed Aslam
    Jan 31, 2011 at 18:10
5

I can't see a decent way to do this through the ways provided by Rails, at least not in -v3.2.14

@Sheharyar Naseer makes reference to the options hash which can be used to solve the problem but not as far as I can see in the way he seems to suggest.

I did this =>

<%= f.fields_for :blog_posts, {:index => 0} do |g| %>
  <%= g.label :gallery_sets_id, "Position #{g.options[:index]}" %>
  <%= g.select :gallery_sets_id, @posts.collect  { |p| [p.title, p.id] } %>
  <%# g.options[:index] += 1  %>
<% end %>

or

<%= f.fields_for :blog_posts do |g| %>
  <%= g.label :gallery_sets_id, "Position #{g.object_name.match(/(\d+)]/)[1]}" %>
  <%= g.select :gallery_sets_id, @posts.collect  { |p| [p.title, p.id] } %>
<% end %>

In my case g.object_name returns a string like this "gallery_set[blog_posts_attributes][2]" for the third field rendered so I just match the index in that string and use it.


Actually a cooler (and maybe cleaner?) way to do it is to pass a lambda and call it to increment.

# /controller.rb
index = 0
@incrementer = -> { index += 1}

And the in the view

<%= f.fields_for :blog_posts do |g| %>
  <%= g.label :gallery_sets_id, "Position #{@incrementer.call}" %>
  <%= g.select :gallery_sets_id, @posts.collect  { |p| [p.title, p.id] } %>
<% end %>
2

Added to fields_for child_index: 0

<%= form_for @person do |person_form| %>
  <%= person_form.fields_for :projects, child_index: 0 do |project_fields| %>
    <%= project_fields.index %>
  <% end %>
<% end %>
2
  • This is the new best answer.
    – genkilabs
    Nov 26, 2019 at 20:25
  • Any one else get duplicated fields with this?
    – user266647
    May 19, 2020 at 23:33
1

I know that this is a bit late but I recently had to do this you can get the index of the fields_for like this

<% f.fields_for :questions do |builder| %>
  <%= render 'some_form', :f => builder, :i => builder.options[:child_index] %>
<% end %>

I hope that this helps :)

0

If you want to have control over the indexes check out the index option

<%= f.fields_for :other_things_attributes, @thing.other_things.build do |ff| %>
  <%= ff.select :days, ['Mon', 'Tues', 'Wed'], index: 2 %>
  <%= ff.hidden_field :special_attribute, 24, index: "boi" %>
<%= end =>

This will produce

<select name="thing[other_things_attributes][2][days]" id="thing_other_things_attributes_7_days">
  <option value="Mon">Mon</option>
  <option value="Tues">Tues</option>
  <option value="Wed">Wed</option>
</select>
<input type="hidden" value="24" name="thing[other_things_attributes][boi][special_attribute]" id="thing_other_things_attributes_boi_special_attribute">

If the form is submitted, params will include something like

{
  "thing" => {
  "other_things_attributes" => {
    "2" => {
      "days" => "Mon"
    },
    "boi" => {
      "special_attribute" => "24"
    }
  }
}

I had to use the index option to get my multi-dropdowns to work. Good luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.