diff --git a/assets/controllers/form_collection_controller.js b/assets/controllers/form_collection_controller.js index 2de5acdb..344f2262 100644 --- a/assets/controllers/form_collection_controller.js +++ b/assets/controllers/form_collection_controller.js @@ -11,10 +11,13 @@ export default class extends Controller { connect() { this.itemsCountValue = this.fieldTargets.length + // A running counter rather than the current count: after a remove, count + 1 can be an + // index a remaining entry still holds, and the two would submit into the same slot. + this.nextIndex = this.fieldTargets.length } addItem() { - const index = this.fieldTargets.length + 1 + const index = this.nextIndex++ const prototype = JSON.parse(this.prototypeValue) const newField = prototype.replace(/__name__/g, index) this.fieldsTarget.insertAdjacentHTML('beforeend', newField)