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
Budget Item: Apples
Budget Item: Oranges
Budget Item: Grapes
wbs_child 2
Budget Item: Wraps
Budget Item: Bagels
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
Budget Item: Apples
Budget Item: Oranges
Budget Item: Grapes
wbs_child 2
Budget Item: Wraps
Budget Item: Bagels
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.
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. 🤦♂️
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 ownwbs_child.id
rendering the next batch ofbudget_items
.bi.options[:child_index]
does not reset on each instance ofform.simple_fields_for
:wbs_child 1
wbs_child 2
Expected behavior
I was expecting
bi.options[:child_index]
to reset on each instance ofform.simple_fields_for
like this:wbs_child 1
wbs_child 2
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.