Ruby on rails 3 Rails:嵌套对象的单选按钮选择
我被这个简单的选择任务困住了。我有以下型号:Ruby on rails 3 Rails:嵌套对象的单选按钮选择,ruby-on-rails-3,radio-button,nested-forms,nested-attributes,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 :choi
# 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
这是表格代码:
<%= 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 %>
多谢各位 这不是答案,但我建议你试试Formtastic。它使处理像这样愚蠢的嵌套模型变得简单:您可以将name选项传递给单选按钮方法:
<%= choice_fields.radio_button :correct, true, :name => "choices_attributes" %>
“选择\u属性”%>
(来自rails文档:
单选按钮(对象名称、方法、标记值、选项={}))
希望这有帮助 这肯定适用于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 %>
“真的”
“假”
要使标签在单击时触发单选按钮,您必须具有
值:无论您的值在
中是什么,您都可以使用单选按钮标签
并将您自己的名称属性传递给它
由于您仍在choice\u字段
范围内,因此您可以访问一些方法,这些方法可以公开您正在处理的对象,这将帮助您正确命名单选按钮标记
以下代码将为单选按钮提供适当的名称,以作为嵌套属性接受:
choice\u字段。index
允许您访问正在创建的资源的正确索引,因此第一个coice将具有名称问题[choices\u attributes][0][correct]
,第二个问题[choices\u attributes][1][correct]
等
choice\u fields.object.correct
允许您访问当前值,以便在编辑表单时填写正确的单选按钮
更新:
上面的解决方案实际上是错误的,它为每个单选按钮提供了不同的名称
属性,因此它们不会一起工作(您可以一次选择多个单选按钮)
我最终采用的解决方案包括以下几行:
<%= radio_button_tag "question[choices_attributes][correct_choice]", choice_fields.index, choice_fields.object.correct %>
然后,我更新了控制器以手动更新正确的选择,如下所示:
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
我也有同样的问题,为什么没有人帮忙回答(你有没有找到这个问题的答案?让我毛骨悚然!你能用更多的信息来解释你的控制器吗?当然!我给控制器添加了一些注释来澄清。我想补充的是,我在Rails 5.1.4中遇到了类似的问题,我必须调用params.permit!才能使用更新的params来创建/保存。
<%= 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