ryanb / nested_form

Rails plugin to conveniently handle multiple models in a single form.
MIT License
1.79k stars 505 forks source link

Nested inputs are being generated with the same name attribute #359

Open elomarns opened 8 years ago

elomarns commented 8 years ago

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.

loicginoux commented 8 years ago

I have the same issue here with every time the id "0" generated, did you find a solution?

naimrajib07 commented 8 years ago

@elomarns and @loicginoux Yes, I fixed this! issue last night. It took whole day to understand the reason! I am going to create pull request.

naimrajib07 commented 8 years ago

I went to create a new pull request, however before do that I checked that anyone has created any PR regarding an issue and found that this issue has been fixed by this PR, https://github.com/ryanb/nested_form/pull/278 I believe when this PR will merge then all issue related this will fix also.

twnaing commented 8 years ago

The #278 build is failing in Travis CI and I do not think @ryanb will merge the PR. @naimrajib07 could you create the PR with your fix?

jjercTK commented 8 years ago

@naimrajib07 @twnaing I have the same issue too. Can you please create a PR or share your code here?

shoffing commented 2 years ago

I'm having this same issue, 5 years later. Does this gem not support a double-nested form (one => many => many)?

All my inputs have names with index [0]...