18

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

#  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

当我创建一个新问题时,我想以嵌套形式不仅指定contentQuestion,甚至指定content3 个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这种方式将它们保存在操作中:

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

非常感谢!

4

4 回答 4

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 将具有 name question[choices_attributes][0][correct],第二个coicequestion[choices_attributes][1][correct]等。

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

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

我最终采用的解决方案如下:

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

这为每个单选按钮提供了与其他单选按钮相同的名称,并且其值等于正确选择的索引。参数哈希最终看起来像这样:

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
于 2017-09-04T02:04:56.650 回答
6

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

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

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

希望这可以帮助。

于 2011-09-11T21:09:47.110 回答
1

这是一个非答案,但我建议您尝试 Formtastic。它使处理像这样 STUPID EASY 的嵌套模型变得容易:https ://github.com/justinfrench/formtastic

于 2011-02-23T18:22:54.370 回答
0

这绝对适用于Rails 4。没有检查其他版本:

您的标签和单选字段应如下所示:

<%= 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 ... %>

于 2017-07-21T20:41:47.947 回答