oli*_*i-g 13 nested-forms radio-button nested-attributes ruby-on-rails-3
我坚持这个简单的选择任务.我有这个型号:
# 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
Run Code Online (Sandbox Code Playgroud)
当我创建一个新的问题,我想指定不仅在嵌套形式content的Question,但即使是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
Run Code Online (Sandbox Code Playgroud)
这是表单代码:
<%= 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 %>
Run Code Online (Sandbox Code Playgroud)
问题是此代码生成三个具有不同名称的单选按钮:您可以选择多个正确答案,这不是正确的行为.三个单选按钮的名称是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
Run Code Online (Sandbox Code Playgroud)
非常感谢你!
您可以使用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 %>
Run Code Online (Sandbox Code Playgroud)
这使每个单选按钮的名称与其他单选按钮的名称相同,并且值等于正确选择的索引.params散列最终看起来像这样:
question: {choices_attributes: {
"0": {//choice 0},
"1": {//choice 1},
etc...
},
correct_choice: //index of correct answer
}
Run Code Online (Sandbox Code Playgroud)
然后我更新了我的控制器以手动更新正确的选择,如下所示:
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
Run Code Online (Sandbox Code Playgroud)
小智 6
您可以将name选项传递给radio_button方法:
<%= choice_fields.radio_button :correct, true, :name => "choices_attributes" %>
Run Code Online (Sandbox Code Playgroud)
(来自rails docs,http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html:radio_button(object_name,method,tag_value,options = {}))
希望这可以帮助.
| 归档时间: |
|
| 查看次数: |
4779 次 |
| 最近记录: |