Active Admin中的嵌套表单
我需要帮助! 我有2个模型用于调查:
class Poll :destroy accepts_nested_attributes_for :poll_questions, :reject_if => lambda { |a| a[:text].blank? }, :allow_destroy => true end
问题的模型如下:(似乎这些关联是正确的)
class PollQuestion :destroy accepts_nested_attributes_for :poll_answers, :reject_if => lambda { |a| a[:text].blank? }, :allow_destroy => true end
另外在活动管理员中:
ActiveAdmin.register Poll do form do |f| f.inputs "Main poll" do f.input :title f.input :description end f.inputs do f.has_many :poll_questions do |question| question.input :text end end f.buttons end end
它有一个美丽的forms,不会创建一个实际的问题对象! 为什么? 我已尽力解决问题,但我失败了。
这可能是因为你对accepts_nested_attributes_for有一个双重关卡。 为什么不为具有许多民意调查答案的民意调查答复创建新模型?
然后,您将在PollResponse类中设置accepts_nested_attributes_for:poll_answers。
然后,您不仅可以对表单问题进行排序,还可以跟踪谁(可能)回答了轮询,以及创建轮询响应的时间。 PollResponses模型也必须属于民意调查,以区分正在回答哪个民意调查。
尝试创建对象,
f.has_many :poll_questions, PollQuestion.new do |question| question.input :text end