Elixir form inputs_for (with one to many realation ship)

Hello Fellas,

I am creating a user facing form for a structure say Book which has one to many relation ship with the structure Chapter. What I want to achieve in a clean manner is to be able to provide the user with the possibility of creating a book and add ass many chapters as necessary in one form. For that I am using the inputs_for. The form is based on a sate-full component procedure based on a the view lifecycle is as follows.
I create the initial changeset using the following data %Book{chapters: [%{temp_id: “sdf3”}]} so that it initially has one slot for a chapter to begin with and I give the possibility to add more through a button.

the button of adding a new chapter slot what it does essentially is getting the changes from the changeset adds one more entry and uses that to create a new changeset like the following.

%Book{chapters: [%{temp_id: “sdf3”, more_data: “maybe”}, %{temp_id: “asdfad”}]}

and this part works just fine (at least as far as I can see)

For the user to be able to delete entries I have another button per form
which essentially filters the form changeset changes again according to the to be deleted temp_id and re created a new changeset.

When I am adding and deleting one record that works fine but the problem is that when I am trying to add more records although the adding part works fine the deleting does not work. I can see the changeset after works being as it should but the ui part does not reflect the change. Even when I do inspect inside the parent form I can see the part being removed in the Form structure but the most parts of the form i.e input for fields etc remain visible in the User Interface.

Any ideas of what is that I am missing here would be greatly appreciated, even if there is a proposal of achieving the same with a different procedure as a whole would be appreciated as I hit my head against the wall at this point.


I’d suggest looking at

Manually maintaining a temp_id shouldn’t be needed.

1 Like