stimulus-rails-nested-form icon indicating copy to clipboard operation
stimulus-rails-nested-form copied to clipboard

Bug with double nested forms

Open wulffeld opened this issue 1 year ago • 4 comments

I think there's bug when using in a form with multiple nested associations. Like a Survey -> Questions -> Answers situation for example.

When clicking Remove the code here https://github.com/stimulus-components/stimulus-rails-nested-form/blob/master/src/index.ts#L34 will grab the first _destroy input which in a double nested situation might be wrong (depends on where you put the remove button) and you'll end up removing a nested child instead of the parent child sometimes.

If the line instead is written like

const input = wrapper.querySelector(":scope > input[name*='_destroy']")

everything works but the precludes that the input element is a direct descendent of the wrapper class. Not sure what the ideal fix would be.

wulffeld avatar Feb 27 '23 20:02 wulffeld

Nice catch! Can you open a PR with examples and the fix?

guillaumebriday avatar Mar 24 '23 23:03 guillaumebriday

Hi, I had the same problem. To resolve the issue, we need to wrap each template within a different div with it's data target and action. And it works

image

loddaa avatar Apr 13 '23 10:04 loddaa

Hi, I had the same problem. To resolve the issue, we need to wrap each template within a different div with it's data target and action. And it works

You have two separate relations, which yes, needs to separate setups. The bug I'm talking about is when you need to have deeply nested forms :)

wulffeld avatar Apr 13 '23 17:04 wulffeld

I ran into this issue as well, but i noticed it where label elements were linking to the first sibling's inputs. What I noticed was that when you have nested nested-form stimulus controllers, the <template>'s NEW_RECORD for new nested associations (2 associations levels deep) all share the same ID as their parent (1st level nested association). it's icky for now but i used stimulus-component's content-loader to load the 2-levels deep

alexspark avatar Jun 22 '23 18:06 alexspark