我有QuestionSet,Question和Choice表
class QuestionSet < ActiveRecord::Base
attr_accessible :name
has_many :questions
end
class Question < ActiveRecord::Base
attr_accessible :name, :correct_answer
belongs_to :question_set
has_many :choices
end
class Choice < ActiveRecord::Base
attr_accessible :question_id, :value
belongs_to :question
end
我想将Choice的id指定为Question的correct_answer。但是由于新的nested_form_for子对象还没有真实的ID,我只能为已经保存的选项分配correct_answer,但不适用于刚刚通过link_to_add创建的选项。以下简化的“查看”代码:
<!-- BTW 'f' below is QuestionSet -->
<%= f.fields_for :questions do |f_f| %>
<!-- Question Name -->
<%= f_f.text_field :name %>
<%= f_f.fields_for :choices do |f_f_f| %>
<!-- Designate as Correct Answer -->
<%= f_f.radio_button :correct_answer, f_f_f.object.id %><!-- THIS IS THE PROBLEM-->
<!-- Choice Name -->
<%= f_f_f.text_field :value %>
<%= f_f_f.link_to_remove "Remove Choice" %>
<% end %>
<%= f_f.link_to_add "Add Choice", :choices %>
<%= f_f.link_to_remove "Remove Question" %>
<% end %>
<%= f.link_to_add "Add Question", :questions %>
同样,代码只能工作,并且只有在选择之前已经保存并因此它已经具有id时,才会将choice-child的id指定为Question的'correct_answer'。但是,它不适用于由link_to_add创建的新创建的选择子项。
任何帮助都会很高兴。谢谢! :)
答案 0 :(得分:0)
为什么不移动:correct_answer字段到Choices并使其成为布尔值?这将简化很多事情