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

Can I jump to view written objective C from Appcelerator Titanium?

objective-c ipad appcelerator appcelerator-mobile

I have legacy iPad app in objective c. I would like give chance to Appcelerator. But I would not like to rewrite whole app. I would prefer to start with replacing only some views.

Php recursive directory creation

php file-io

I must create recursive directories and create a file for every folder. I want like this:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.