heartcombo / simple_form

Forms made easy for Rails! It's tied to a simple DSL, with no opinion on markup.
http://blog.plataformatec.com.br/tag/simple_form
MIT License
8.21k stars 1.31k forks source link

options[:child_index] does not reset on subsequent simple_fields_for instances #1760

Closed jasper502 closed 2 years ago

jasper502 commented 2 years ago

Environment

Current behavior

I am implementing a nested hierarchy simple_fields_for like this:

form.simple_fields_for :budget_items, budget.budget_items.where(wbs_item_id: wbs_child.id) do |bi|

The next level up I am looping though each wbs_child which is the next level up in the hierarchy in the view. form.simple_fields_for is called multiple times each with it's own wbs_child.id rendering the next batch of budget_items.

bi.options[:child_index] does not reset on each instance of form.simple_fields_for:

wbs_child 1

  1. Budget Item: Apples
  2. Budget Item: Oranges
  3. Budget Item: Grapes

wbs_child 2

  1. Budget Item: Wraps
  2. Budget Item: Bagels
  3. Budget Item: Bread

Expected behavior

I was expecting bi.options[:child_index] to reset on each instance of form.simple_fields_for like this:

wbs_child 1

  1. Budget Item: Apples
  2. Budget Item: Oranges
  3. Budget Item: Grapes

wbs_child 2

  1. Budget Item: Wraps
  2. Budget Item: Bagels
  3. Budget Item: Bread

I have a work around adding a manual index | bi, bi_index | with an increment. I am trying to see why this would not reset to 0 by default? If this is the intended behaviour perhaps document this clearly.

jasper502 commented 2 years ago

After continuing on this I realize this the expected behaviour - I was not considering that the child index is required to be unique for the nested attributes etc. 🤦‍♂️