Rails:嵌套对象的单选按钮选择

时间:2011-02-23 12:19:16

标签: ruby-on-rails-3 radio-button nested-forms nested-attributes

我坚持这个简单的选择任务。我有这个型号:

#  id         :integer(4)      not null, primary key
#  category   :string(255)
#  content    :text
class Question < ActiveRecord::Base
    has_many :choices, :dependent => :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 < ActiveRecord::Base
    belongs_to :question
end

当我创建新问题时,我想以嵌套的形式指定content的{​​{1}},甚至是3 Question个对象的content ,并使用单选按钮选择,其中一个是Answer答案。在控制器的correct操作中,我有:

new

这是表格代码:

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 => @question }
    end
end

问题是这段代码会生成三个不同名称的单选按钮:您可以选择多个正确答案,这不是正确的行为。三个单选按钮的名称为<%= simple_form_for @question do |question_form| %> <%= question_form.error_notification %> <div class="inputs"> <%= question_form.input :content, :label => 'Question' %> <%= question_form.input :category, :collection => get_categories, :include_blank => false %> <% @question.choices.each do |choice| %> <%= question_form.fields_for :choices, choice do |choice_fields| %> <%= choice_fields.input :content, :label => 'Choice' %> <%= choice_fields.radio_button :correct, true %> <%= choice_fields.label :correct, 'Correct Answer' %> <% end %> <% end %> </div> <div class="actions"> <%= question_form.button :submit %> </div> <% end %> question[choices_attributes][0][correct]question[choices_attributes][1][correct]

问题是:如何创建三个具有相同名称的单选按钮,以便选择一个且只有一个正确的答案?如何创建正确的question[choices_attributes][2][correct]数组,以便以这种方式将其保存在params操作中:

create

非常感谢!

4 个答案:

答案 0 :(得分:9)

您可以使用radio_button_tag并将其传递给您自己的名称属性。

由于您仍处于choice_fields范围内,因此您可以访问一些可以公开您正在处理的对象的方法,这有助于您正确命名radio_button_tag

以下代码将为您的单选按钮指定要作为嵌套属性接受的正确名称:

<%= radio_button_tag "question[choices_attributes][#{choice_fields.index}][correct]", true, choice_fields.object.correct %>

choice_fields.index可让您访问正在创建的资源的正确索引,因此第一个coice的名称为question[choices_attributes][0][correct],第二个为question[choices_attributes][1][correct]等。

choice_fields.object.correct可让您访问当前值,以便为编辑表单填写正确的单选按钮。

<强>更新 上面的解决方案实际上是错误的,它为每个单选按钮提供了不同的name属性,因此它们不会一起工作(您可以一次选择多个单选按钮)。

我最终选择的解决方案是以下几行:

<%= radio_button_tag "question[choices_attributes][correct_choice]", choice_fields.index, choice_fields.object.correct %>

这使每个单选按钮的名称与其他单选按钮的名称相同,并且值等于正确选择的索引。 params散列最终看起来像这样:

question: {choices_attributes: {
        "0": {//choice 0},
        "1": {//choice 1},
        etc...
    },
    correct_choice: //index of correct answer
}

然后我更新了我的控制器以手动更新正确的选择,如下所示:

def create

    # Find the index of the correct choice in the params hash
    correct_index = params[:question][:correct_choice]

    # mark the question at the correct index as correct
    params[:question][:choiceses_attributes][correct_index][:correct] = true

    # Use the updated params hash to create a new Question/update an existing Question

    @question = Question.new(params[:question])
    # render or redirect stuff....
end

答案 1 :(得分:6)

您可以将name选项传递给radio_button方法:

<%= choice_fields.radio_button :correct, true, :name => "choices_attributes" %>

(来自rails docs,http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html:     radio_button(object_name,method,tag_value,options = {}))

希望这会有所帮助。

答案 2 :(得分:2)

这是一个非答案,但我建议你试试Formtastic。它可以处理嵌套模型,例如STUPID EASY:https://github.com/justinfrench/formtastic

答案 3 :(得分:0)

这肯定适用于 Rails 4 。 Haven没有检查过其他版本:

您的标签&amp;无线电领域应如下所示:

<%= choice_fields.radio_button :correct, true %>
<%= choice_fields.label :correct, value: true, do %>
  'True'
<% end %>
<%= choice_fields.radio_button :correct, false %>
<%= choice_fields.radio_button :correct, value: false, do %>
  'False'
<% end %>

要使标签在点击时触发单选按钮,您必须value: whatever_your_value_is中有<%= choice_fields.label ... %>