fix(rules): keep dynamically added nested-attribute keys numeric (#3444)

* fix(rules): keep dynamically added nested-attribute keys numeric

Rails strong params only treat integer-keyed hashes as nested attributes
(ActionController::Parameters.nested_attribute? matches /\A-?\d+\z/), so
rules_controller.js keys of the form "new_1"/"new_2" (introduced in
457698f, PR #3397) were silently dropped on submit: creating a rule from
the modal failed with "must have at least one action" even though an
action was present, and the re-rendered form lost the entered rows.

Date.now() alone can repeat within the same millisecond and a small
counter alone can collide with the numeric indexes Rails assigns to
persisted rows on edit forms, so combine them (Date.now() * 1000 + seq)
in both rules_controller.js and rule/conditions_controller.js.

Adds a system test that creates a rule through the modal with
dynamically added rows; no system test previously covered this path,
which is why the regression shipped.

* Fix rule creation regression

---------

Co-authored-by: Instinct Agent <agent@instinct.com>
This commit is contained in:
Juan José Mata
2026-09-08 05:10:03 +02:00
committed by GitHub
co-authored by Instinct Agent
parent de374962f0
commit fb01bbbae3
3 changed files with 25 additions and 6 deletions
@@ -123,7 +123,8 @@ export default class extends Controller {
}
#uniqueKey() {
return Date.now();
this.keySequence = (this.keySequence ?? 0) + 1;
return Date.now() * 1000 + this.keySequence;
}
#toggleValueFieldVisibility() {
@@ -50,12 +50,8 @@ export default class extends Controller {
}
#uniqueKey() {
// Prefixed so it can never collide with the numeric indexes Rails
// assigns to already-persisted conditions/actions when rendering an
// edit form (0, 1, 2, ...). A plain monotonic counter starting at 1
// would otherwise reuse index 1 and clobber an existing nested record.
this.keySequence = (this.keySequence ?? 0) + 1;
return `new_${this.keySequence}`;
return Date.now() * 1000 + this.keySequence;
}
// Updates the prefix visibility of all conditions and condition groups