我建立了一个视图来创建问题和答案。这是我的问答模型:
class Question < ActiveRecord::Base
attr_accessible :content
belongs_to :topic
belongs_to :user
belongs_to :question_type
has_many :answers, dependent: :destroy
accepts_nested_attributes_for :answers
end
class Answer < ActiveRecord::Base
attr_accessible :content
belongs_to :question
end
我的答案模型还有一个名为correct
type boolean 的属性,如果哪个答案正确,则设置为 true,默认为 false。我已经建立了一个表格来创建一个真/假问题:
<%= simple_form_for @question do |q| %>
<%= q.input :content, input_html: { rows: 3, class: 'span6' } %>
<%= q.input :mark, input_html: { class: 'span1' } %>
<%= q.association :topic %>
<%= q.association :question_type %>
<%= q.simple_fields_for :answers do |a| %>
<%= a.input :correct, collection: [[true, 'True'], [false, 'False']],
as: :radio_buttons,
label: 'Answer',
value_method: :first,
label_method: :last,
item_wrapper_class: 'inline'
%>
<% end %>
<% end %>
在视图中呈现时的正确属性具有以下 html:
<label class="radio inline"><input class="radio_buttons optional" id="question_answers_attributes_0_correct_true" name="question[answers_attributes][0][correct]" type="radio" value="true">True</label>
<label class="radio inline"><input checked="checked" class="radio_buttons optional" id="question_answers_attributes_0_correct_false" name="question[answers_attributes][0][correct]" type="radio" value="false">False</label>
现在在控制器中,我想检查选中了哪个单选按钮及其值。有什么方法可以检查是否correct
检查了属性及其在控制器中的值?