Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

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

Open
elomarns opened this issue Sep 16, 2015 · 6 comments
Open

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

elomarns opened this issue Sep 16, 2015 · 6 comments

Comments

@elomarns
Copy link

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
Copy link

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

@naimrajib07
Copy link

@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
Copy link

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, #278 I believe when this PR will merge then all issue related this will fix also.

@twnaing
Copy link

twnaing commented Feb 2, 2016

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
Copy link

jjercTK commented Feb 5, 2016

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

@shoffing
Copy link

shoffing commented Aug 4, 2021

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]...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants