Skip to content

Commit 42028bc

Browse files
committed
Merge pull request ryanb#266 from kirel/fix-parent-has-no-input
Fix id generation if parent has no input
2 parents a25eebe + 289634d commit 42028bc

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

vendor/assets/javascripts/jquery_nested_form.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@
1414

1515
// Make the context correct by replacing <parents> with the generated ID
1616
// of each of the parent objects
17-
var context = ($(link).closest('.fields').closestChild('input, textarea, select').eq(0).attr('name') || '').replace(new RegExp('\[[a-z_]+\]$'), '');
17+
var context = ($(link).closest('.fields').closestChild('input, textarea, select').eq(0).attr('name') || '').replace(/\[[a-z_]+\]$/, '');
18+
19+
// If the parent has no inputs we need to strip off the last pair
20+
var current = content.match(new RegExp('\\[([a-z_]+)\\]\\[new_' + assoc + '\\]'))[1];
21+
if (current) {
22+
context = context.replace(new RegExp('\\['+current+'\\]\\[(new_)?\\d+\\]$'), '');
23+
}
1824

1925
// context will be something like this for a brand new form:
2026
// project[tasks_attributes][1255929127459][assignments_attributes][1255929128105]

vendor/assets/javascripts/prototype_nested_form.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ document.observe('click', function(e, el) {
88

99
// Make the context correct by replacing <parents> with the generated ID
1010
// of each of the parent objects
11-
var context = (el.getOffsetParent('.fields').firstDescendant().readAttribute('name') || '').replace(new RegExp('\[[a-z_]+\]$'), '');
11+
var context = (el.getOffsetParent('.fields').firstDescendant().readAttribute('name') || '').replace(/\[[a-z_]+\]$/, '');
12+
13+
// If the parent has no inputs we need to strip off the last pair
14+
var current = content.match(new RegExp('\\[([a-z_]+)\\]\\[new_' + assoc + '\\]'))[1];
15+
if (current) {
16+
context = context.replace(new RegExp('\\['+current+'\\]\\[(new_)?\\d+\\]$'), '');
17+
}
1218

1319
// context will be something like this for a brand new form:
1420
// project[tasks_attributes][1255929127459][assignments_attributes][1255929128105]

0 commit comments

Comments
 (0)