Closed gabydi106 closed 3 years ago
Hi there, thank you for your first issue and taking the time to write a good and clear explanation.
The problem is your sales_order_params
: because you do not allow the :id
as a parameter for your sales-order-items, rails has no way of knowing if the received items match the existing ones and will assume they are new items.
So you will have to change your sales_order_params
as follows:
:sales_order_items_attributes => [:id, :item_id, :price, :quantity, :_destroy]
So to be extra clear: I added
:id
to know if a nested item existed before or not_destroy
to know if the item should be destroyed.Thank you so much for pointing this out to me. I have amended my code and it works smoothly now. I appreciate your detailed explanation as well. Today I learned something new :1st_place_medal: Thank you!
Issue resolved, will close with comment.
The nested form works great when creating new records. However when I edit the record, it creates duplicate child items even if I do not mean to add new items.
How do I prevent it from creating new child items when updating the Sales Order?
Sales Order Controller (parent)