Get Current Child Index On Fields_for In Rails 3.2

Posted By Weston Ganger

It is extremely hard to get the index value of the object in a fields_for or simple_fields_for in Rails 3.2 (Rails 4 has this feature).

You can add this monkey patch to get access to those index values.

One gotcha is that it will only have an index value if the object was not dynamically added (like with cocoon or nested forms)

<% f.fields_for :documents do |x| %>
  <%= x.index %>
<% end %>

Related External Links:

Article Topic:Software Development - Ruby / Rails

Date:October 03, 2015