I have a Ruby on Rails app that has a form with nested attributes. This form allows the dynamic addition of fields with some javascript. The form also submits itself on blur via AJAX.
Everything is working great, with the exception of one issue that centres around the dynamic addition of fields.
When the dynamic fields are added via Javascript, they are assigned a unique ID. However, the ID and pattern of names for form fields obviously does not match what rails would generate once the fields have been saved.
Because of the AJAX form submit, the dynamically added fields create a new DB entry every time the form is changed.
I need to find a way to gracefully handle the fact that I need to retrieve the rails generated form and modify/replace the one on the page after an AJAX submit – otherwise new fields will continue to replicate!
You can see my code at: https://github.com/michaelward82/WeddingPlanner/tree/UI
(Apologies if my Rails code is not the cleanest, this is my first genuine attempt at a real rails project and I’m still learning the patterns)
I think what you’re missing is a
:dependent => :delete_allparameter on thebelongs_toassociation. That makes Rails delete the old budget items when a new set is saved: