title: How do I number nested fields?
body: <p>I'm building nested fields with a <code>has_many</code>, <code>belongs_to</code> relationship like so:</p>
  
  <p><code>app/controllers/widgets_controller.rb</code></p>
  
  <pre><code>def new
    @widget = Widget.new
    3.times { @widget.gizmos.build }
  end
  </code></pre>
  
  <hr>
  
  <p><code>app/views/widgets/_form.html.erb</code></p>
  
  <pre><code>&lt;%= form_for @widget do |f| %&gt;
    ...
    &lt;%= f.fields_for :gizmos do |b| %&gt;
      &lt;%= render 'gizmo_fields', f: b %&gt;
    &lt;% end -%&gt;
    ...
  &lt;% end -%&gt;
  </code></pre>
  
  <hr>
  
  <p><code>app/views/widgets/_gizmo_fields.html.erb</code></p>
  
  <pre><code>&lt;%= f.label :name "Gizmo -NUMBER-" %&gt;
  &lt;%= f.text_field :name %&gt;
  </code></pre>
  
  <hr>
  
  <p>How can I programatically determine the number <code>gizmo</code> out of the total number of <code>widget.gizmos</code>. Such that each the output would look like:</p>
  
  <pre><code>Gizmo 1 |_____text_____|
  Gizmo 2 |_____text_____|
  Gizmo 3 |_____text_____|
  </code></pre>
  
  <p>What I would like to avoid:</p>
  
  <ul>
  <li><strong>Using Javascript</strong> - I know I can number these with vanilla javascript or jquery. I would like a pure ruby/rails solution so that not only would the code be easier to follow, but I hope to learn more about nested fields.</li>
  <li><strong>Incrementing a variable</strong> - Again, I'd like to learn more about the context of nested fields and see if there is a more dynamic and concrete way to number the nested fields.</li>
  </ul>
  
  <p>What I already know:</p>
  
  <ul>
  <li>I can access the current instance of <code>gizmo</code> through <code>f.object</code></li>
  <li>I cannot access <code>f.object.widget</code> on the <code>new</code> action, because the records are not yet saved</li>
  </ul>