From 70a7197a032d5dd1b7f381cae50741e8b02674c1 Mon Sep 17 00:00:00 2001 From: groe Date: Tue, 12 Feb 2013 00:05:42 +0100 Subject: [PATCH] Allow nested associations with same name #237 --- vendor/assets/javascripts/jquery_nested_form.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/vendor/assets/javascripts/jquery_nested_form.js b/vendor/assets/javascripts/jquery_nested_form.js index 3b8a5b39..a418bc9f 100644 --- a/vendor/assets/javascripts/jquery_nested_form.js +++ b/vendor/assets/javascripts/jquery_nested_form.js @@ -21,18 +21,22 @@ // or for an edit form: // project[tasks_attributes][0][assignments_attributes][1] if (context) { + var prevParentsId = ""; + var prevParentsName = ""; var parentNames = context.match(/[a-z_]+_attributes(?=\]\[(new_)?\d+\])/g) || []; var parentIds = context.match(/[0-9]+/g) || []; for(var i = 0; i < parentNames.length; i++) { if(parentIds[i]) { content = content.replace( - new RegExp('(_' + parentNames[i] + ')_.+?_', 'g'), + new RegExp('(_' + prevParentsId + parentNames[i] + ')_.+?_', 'g'), '$1_' + parentIds[i] + '_'); + prevParentsId += "_" + parentNames[i] + "_" + parentIds[i] + "_"; content = content.replace( - new RegExp('(\\[' + parentNames[i] + '\\])\\[.+?\\]', 'g'), + new RegExp('(' + prevParentsName + '\\[' + parentNames[i] + '\\])\\[.+?\\]', 'g'), '$1[' + parentIds[i] + ']'); + prevParentsName += "\\[" + parentNames[i] + "\\]\\[" + parentIds[i] + "\\]"; } } }