index is nil in a fields_for nested attributes

06 May 2018 • Tags: railsnested attributesaccepts_nested_attributes_forviews

Just hit an interesting bug and thankfully fixed it quick enough.

I have a model with entries that I render form fields for and use the index for something. The view looks like this and you get the index for free.

= form.fields_for :entries, form.object.entries do |entry_fields|
  = entry_fields.index
  / ...

But the index was nil everytime which is contradicing the documentation on fields_for. What I was missing in my model was accepts_nested_attributes_for :entries to get the index set

  class Transaction
    include Mongoid::Document
    embeds_many :entries
    accepts_nested_attributes_for :entries