Nested form in Active Admin

青春壹個敷衍的年華 提交于 2019-12-13 05:21:01

问题


I need help! I have 2 models for a Survey:

class Poll < ActiveRecord::Base

has_many :poll_questions, :dependent => :destroy
accepts_nested_attributes_for :poll_questions, :reject_if => lambda { |a| a[:text].blank?    }, :allow_destroy => true

end

There is model for questions as follows: (it seems these assocciations are correct)

class PollQuestion < ActiveRecord::Base
belongs_to :poll
has_many :poll_answers, :dependent => :destroy
accepts_nested_attributes_for :poll_answers, :reject_if => lambda { |a| a[:text].blank? }, :allow_destroy => true

end

In addition in active Admin:

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

It has a beautiful form that doesnt create an actual question object! why? I've tried my best to solve the problem, but I've failed.


回答1:


It's probably because you've got a double level for accepts_nested_attributes_for. Why not create a new model for Poll responses which has many Poll answers?

You would then set up an accepts_nested_attributes_for :poll_answers within the PollResponse class.

Then you can not only sort your form problems out, but also track who answered the poll (potentially) and when the poll response was created. The PollResponses model would also have to belong to Polls to differentiate which Poll was being answered.




回答2:


Try by creating object,

f.has_many :poll_questions, PollQuestion.new do |question|
  question.input :text
end


来源:https://stackoverflow.com/questions/8224884/nested-form-in-active-admin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!