Note that there are some explanatory texts on larger screens.

plurals
  1. PORails: radio button selection for nested objects
    primarykey
    data
    text
    <p>I'm stuck with this simple selection task. I have this models:</p> <pre><code># id :integer(4) not null, primary key # category :string(255) # content :text class Question &lt; ActiveRecord::Base has_many :choices, :dependent =&gt; :destroy accepts_nested_attributes_for :choices end # id :integer(4) not null, primary key # content :text # correct :boolean(1) # question_id :integer(4) class Choice &lt; ActiveRecord::Base belongs_to :question end </code></pre> <p>When I create a new question, I want to specify in a nested form not only the <code>content</code> of the <code>Question</code>, but even the <code>content</code> of 3 <code>Answer</code> objects, and select with a radio button which one is the <code>correct</code> answer. In the <code>new</code> action of the controller, I have this:</p> <pre><code>def new @title = "New Question" @question = Question.new 3.times { @question.choices.build } respond_to do |format| format.html # new.html.erb format.xml { render :xml =&gt; @question } end end </code></pre> <p>This is the form code:</p> <pre><code>&lt;%= simple_form_for @question do |question_form| %&gt; &lt;%= question_form.error_notification %&gt; &lt;div class="inputs"&gt; &lt;%= question_form.input :content, :label =&gt; 'Question' %&gt; &lt;%= question_form.input :category, :collection =&gt; get_categories, :include_blank =&gt; false %&gt; &lt;% @question.choices.each do |choice| %&gt; &lt;%= question_form.fields_for :choices, choice do |choice_fields| %&gt; &lt;%= choice_fields.input :content, :label =&gt; 'Choice' %&gt; &lt;%= choice_fields.radio_button :correct, true %&gt; &lt;%= choice_fields.label :correct, 'Correct Answer' %&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;/div&gt; &lt;div class="actions"&gt; &lt;%= question_form.button :submit %&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre> <p>The problem is that this code produce three radio buttons with different names: you can select more than one correct answer, and this is not the correct behaviour. The names of the three radio buttons are <code>question[choices_attributes][0][correct]</code>, <code>question[choices_attributes][1][correct]</code> and <code>question[choices_attributes][2][correct]</code>.</p> <p>The question is: how can I create three radio buttons with the same name, in order to select one and only one correct answer? How can I create a correct <code>params</code> array, in order to save them in the <code>create</code> action in this way:</p> <pre><code>def create @question = Question.new(params[:question]) # render or redirect stuff.... end </code></pre> <p>Thank you very much!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload