Description
I'm building an application where users can create surveys and add questions to them. A question can have many possible answers, each one being represented as a radio button for its parent question when the survey is being answered. However, possible answers can also have questions. In this case, these questions are conditional questions. They will be shown only if its parent possible answer were selected when someone answered that question.
These are the models which implement this structure:
class Survey < ActiveRecord::Base
has_many :questions
accepts_nested_attributes_for :questions
end
class Question < ActiveRecord::Base
belongs_to :survey
belongs_to :parent_possible_answer, class_name: "PossibleAnswer"
has_many :possible_answers, foreign_key: :parent_question_id
accepts_nested_attributes_for :possible_answers
end
class PossibleAnswer < ActiveRecord::Base
belongs_to :parent_question, class_name: "Question"
has_one :conditional_question, class_name: "Question",
foreign_key: :parent_possible_answer_id
accepts_nested_attributes_for :conditional_question
end
However, when I add a possible answer to a conditional question, each nested input generated has the same name attribute. For example:
survey[questions_attributes][0][possible_answers_attributes][1442346847786][conditional_question_attributes][possible_answers_attributes][1442346847786][content]
I believe the problem is on the following snippet from jquery_nested_form.js:
if (context) {
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'),
'$1_' + parentIds[i] + '_');
content = content.replace(
new RegExp('(\\[' + parentNames[i] + '\\])\\[.+?\\]', 'g'),
'$1[' + parentIds[i] + ']');
}
}
}
jquery_nested_form.js creates a variable named content, which contains the template for the new nested fields. This variable doesn't have ids, instead it presentes its associations as "new_association". So the above snippet of code takes the current ids from the context, and replace it on the content variable. But in my case, I have the following string for the actual input on the content variable:
<input class="string required possible_answer_content" type="text" name="survey[questions_attributes][0][possible_answers_attributes][new_possible_answers][conditional_question_attributes][possible_answers_attributes][new_possible_answers][content]" id="survey_questions_attributes_0_possible_answers_attributes_new_possible_answers_conditional_question_attributes_possible_answers_attributes_new_possible_answers_content" />
And this is the context:
"survey[questions_attributes][0][possible_answers_attributes][1442422446853][conditional_question_attributes]"
On the content variable, the "new_possible_answer" string appears after a question and also after a conditional question on the name attribute. So each possible answer added for a conditional question receives the same id of the previous possible answer, instead of receiving a new id.