nested ruby params hash and radio buttons dont play along well

In a Form, i want users to add one or many Answers on a single submit. To achieve this, i have a params hash containing an array of hashes. An example would look like this:

screen[question][unique_answer][][text]
screen[question][unique_answer][][next_screen]

and the expected hash should be something like:

{
...
"unique_answer" => [ { "text"=>"positive answer", "next_screen"=>"no_link" }, { "text" => "negative answer", "next_screen"=>"5" } ]
...
} 

the Answer is a fieldset containing an text input and some radio inputs. The user can add a new Answer; this is done via javascript (it copies the fieldset, changes the ids so they don't collide and adds it to the DOM).

I have successfully used this technique several times before with fieldsets which only have text inputs, but now, when radio buttons are included, the browser take all of the radiobuttons as if they were the same group and allows only to check one trough the whole answers.

Moreover, the submitted params includes the key "next_screen" only on the first array object. it looks like this:

{
...
"unique_answer" => [ { "text"=>"positive answer", "next_screen"=>"no_link" }, { "text" => "negative answer" } ]
...
} 

an extract of the html form follows:

<fieldset class="answer">
    <ol>
        <li id="screen_question_unique_answer_answer_text_input" class="unique-answer--answer string input required stringish">
            <label for="screen_question_unique_answer_answer_text" class=" label">Answer text</label>
            <input type="text" name="screen[question][unique_answer][][text]" id="screen_question_unique_answer_answer_text">
        </li>

        <li id="screen_question_unique_answer__next_screen_input" class="unique-answer--answer next_screen_picker radio input required">
            <fieldset class="choices">
                <legend class="label">
                    <label>Next Screen</label>
                </legend>
                <ol class="choices-group">
                    <li class="choice">
                        <label for="screen_question_unique_answer__next_screen_5">
                            <input type="radio" value="5" name="screen[question][unique_answer][][next_screen]" id="screen_question_unique_answer__next_screen_5">Blanditiis necessitatibus aliquam recusandae doloremque suscipit.
                        </label>
                    </li>
                    <li class="choice">
                        <label for="screen_question_unique_answer__next_screen_no_link">
                            <input type="radio" value="no_link" name="screen[question][unique_answer][][next_screen]" id="screen_question_unique_answer__next_screen_no_link">Back to Inbox
                        </label>
                    </li>
                </ol>
            </fieldset>
        </li>
    </ol>
</fieldset>

<fieldset class="answer">
    <ol>
        <li id="screen_question_unique_answer_1_answer_text_input" class="unique-answer--answer string input required stringish">
            <label for="screen_question_unique_answer_1_answer_text" class=" label">Answer text</label>
            <input type="text" name="screen[question][unique_answer][][text]" id="screen_question_unique_answer_1_answer_text">
        </li>

        <li id="screen_question_unique_answer_1_next_screen_input" class="unique-answer--answer next_screen_picker radio input required">
            <fieldset class="choices">
                <legend class="label">
                    <label>Next Screen</label>
                </legend>
                <ol class="choices-group">
                    <li class="choice">
                        <label for="screen_question_unique_answer_1_next_screen_5">
                            <input type="radio" value="5" name="screen[question][unique_answer][][next_screen]" id="screen_question_unique_answer_1_next_screen_5">Blanditiis necessitatibus aliquam recusandae doloremque suscipit.
                        </label>
                    </li>
                    <li class="choice">
                        <label for="screen_question_unique_answer_1_next_screen_no_link">
                            <input type="radio" value="no_link" name="screen[question][unique_answer][][next_screen]" id="screen_question_unique_answer_1_next_screen_no_link">Back to Inbox
                        </label>
                    </li>
                </ol>
            </fieldset>
        </li>
    </ol>
</fieldset>

Does anyone had this problem? Any comments on how to solve this?

Answers


Well after hitting the wall several times i ended up following KappaNossi advice and added an index to the params., so now the hash looks like this

screen[question][unique_answer][1][next_screen]

Altought this doesnt sound like much of a work, all other form fields were working nicely with the technique described above and had indeed to write an specific implementation for this case.


Need Your Help

Creating a stored procedure in SQL Server 2008 that will do a "facebook search"

sql-server-2008 stored-procedures full-text-search

I'm trying to implement a facebook search in my system (auto suggest while typing).

“Couldn't send a valid signature” error when using CloudKit without iCloud account

ios icloud cloudkit

I have submitted an iOS app that uses CloudKit to Apple. It's being rejected because it requires user to sign into iCloud for access. Apple's reasoning is that even without iCloud sign-in, the public