ActiveAdmin + Formtastic中的嵌套表单

时间:2019-01-20 09:31:08

标签: ruby-on-rails activeadmin formtastic

我想创建一个表格来生成1个问题和4个答案。我可以毫无疑问地创建问题,但是无法生成嵌套答案。

为此,我通过ActiveAdmin创建了部分(_newq.html.erb)加载。

问题控制者:

permit_params :name,
              :explanation,
              :variant,
              :usage,
              :published,
              :topic_id,
              :context,
              choices_attributes: [:id, :name, :correct, :_destroy]

_newq.html.erb部分:

<%= form_with(url: admin_questions_path, local: true, action: :create) do |f| %>
  <%= f.fields_for :question do |s| %>
    <%= s.label :name %>:
      <%= s.text_field :name %><br />
    <%= s.label :topic_id %>:
      <%= s.collection_select(:topic_id, Topic.all, :id, :exam_with_topic) %><br />
    <%= s.label :published %>:
      <%= s.check_box :published %><br />
    <%= s.label :context %>:
      <%= s.text_area :context %><br />
    <%= s.label :explanation %>:
      <%= s.text_area :explanation %><br />
    <%= s.label :variant %>:
      <%= s.select( :variant, ["Multiple Choice"]) %><br />
              <!--f.input :variant, :as => :select, :collection => ["fill", "Multiple Choice"], label: "Question Type"-->
    <%= s.label :usage %>:
      <%= s.select( :usage, ["Free Quiz"]) %><br />

    <%= s.fields_for :choices_attributes do |c| %>
      <%= c.label "Answer 1" %>:
        <%= c.text_field :name, :value => "answer test" %><br />
      <%= c.label "Correct?" %>:
        <%= c.check_box :correct %><br />
    <% end %>    


  <% end %>
  <%= f.submit %>
<% end %>

如果我删除“ choices_attributes”部分,则可以毫无问题地创建问题,但是尝试也创建嵌套的选择将通过以下消息返回500错误:TypeError (no implicit conversion of Symbol into Integer)

我错过了什么吗?或者这是不可能的吗?

1 个答案:

答案 0 :(得分:1)

这是可能的。我建议您使用Active Admin的dsl来构建表单,请参见https://activeadmin.info/5-forms.html#nested-resources(即使对于高度定制的表单,我也不再使用.erb局部变量)。确保在模型之间建立关联,并在需要的地方添加accepts_nested_attributes。

您的表单定义可能如下所示。

form do |f|
  f.semantic_errors *f.object.errors[:base]
  inputs 'Question' do
    input :name
    input :explanation, :as => :text_area
    input :variant, :as => :select, :collection => Variant.for_html_select
    input :usage
    input :published
    input :topic, :as => select, :collection => Topic.all # make sure you put the has_many and belongs_to in your Question and Topic model
    input :context
  end
  inputs "Answers" do
    has_many :choices, allow_destroy: true do |c|
      c.input :name, :label => "Answer"
      c.input :correct, :label => "Correct?", :as => :boolean
  end
  actions
end

别忘了

class Question < ApplicationRecord
  has_many :choices
  accepts_nested_attributes_for :choices, :allow_destroy => true
  belongs_to :topic

class Choice < ApplicationRecord
  belongs_to :question

class Topic < ApplicationRecord
  has_many :questions

要将答案限制为4,我将在问题模型中添加自定义验证: 和

class Question < ApplicationRecord
  validate :answer_check

  def answer_check
    errors.add(:base, "Needz 4 answerz") if choices.nil? || choices.length != 4
  end
end

class Variant < ApplicationRecord
  def self.for_html_select
    [
       ["Multiple Choice","fill"],
       ["Other variant","other"],
    ]
  end
end

祝你好运!