Reputation: 171
I want to create a form to generate 1 question with 4 answers. I'm able to create the question no problem, but generating the nested answers isn't working.
To do this, I've created a partial (_newq.html.erb) loading through ActiveAdmin.
The question controller:
permit_params :name,
:explanation,
:variant,
:usage,
:published,
:topic_id,
:context,
choices_attributes: [:id, :name, :correct, :_destroy]
The _newq.html.erb partial:
<%= 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 %>
If I remove the "choices_attributes" section, I'm able to create questions with no problems, but trying to also create nested choices returns 500 error with this msg: TypeError (no implicit conversion of Symbol into Integer)
Am I missing something or is this just not possible?
Upvotes: 0
Views: 1863
Reputation: 2182
This is possible. I suggest you use Active Admin's dsl for building the form, see https://activeadmin.info/5-forms.html#nested-resources ( I stopped using .erb partials, even for highly customized forms ). Make sure you set up the associations between your models and add accepts_nested_attributes where needed.
Your form definition could look like this.
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
Don't forget the
class Question < ApplicationRecord
has_many :choices
accepts_nested_attributes_for :choices, :allow_destroy => true
belongs_to :topic
and
class Choice < ApplicationRecord
belongs_to :question
and
class Topic < ApplicationRecord
has_many :questions
To limit the answers to 4, I would add custom validation in the question model: and
class Question < ApplicationRecord
validate :answer_check
def answer_check
errors.add(:base, "Needz 4 answerz") if choices.nil? || choices.length != 4
end
end
and
class Variant < ApplicationRecord
def self.for_html_select
[
["Multiple Choice","fill"],
["Other variant","other"],
]
end
end
Good luck!
Upvotes: 1