我正在尝试为调查构建一个嵌套表单,并且在fields_for
块中遇到我的params哈希值缺失问题。
我有以下型号。该应用的工作方式是管理员创建MiniPost
及其MiniPostQuestions
。然后,客户端使用MiniPostSurvey
之一创建新的MiniPosts
。然后,客户端会将SurveyInvites
通过电子邮件发送给用户。当用户回复时,系统会为调查中的每个SurveyResponse
创建新的QuestionAnswer
以及新的MiniPostQuestion
。
class MiniPost < ActiveRecord::Base
has_many :mini_post_questions
has_many :question_answers, through: :mini_post_questions
has_many :mini_post_surveys
belongs_to :employer
def questions
mini_post_questions
end
def surveys
mini_post_surveys
end
end
class MiniPostQuestion < ActiveRecord::Base
has_many :question_answers
belongs_to :mini_post
belongs_to :employer
def answers
question_answers
end
end
class MiniPostSurvey < ActiveRecord::Base
belongs_to :mini_post
belongs_to :employer
belongs_to :company
has_many :survey_invites
has_many :survey_responses, through: :survey_invites
def questions
mini_post.questions
end
end
class SurveyInvite < ActiveRecord::Base
belongs_to :mini_post_survey
has_many :survey_responses
def responses
survey_responses
end
end
class SurveyResponse < ActiveRecord::Base
belongs_to :survey_invite
has_many :question_answers
has_many :mini_post_questions, through: :question_answers
end
class QuestionAnswer < ActiveRecord::Base
belongs_to :survey_response
belongs_to :mini_post_question
validates :answer, presence: true
end
这是我的回复表。
<div class="form-box">
<%= form_for @response, url: "/survey?key=#{@invite.key}", method: :post, html: { role: 'form' } do |f| %>
<% @questions.each do |question| %>
<div class="form-group">
<%= f.fields_for :mini_post_questions, question do |q| %>
<%= q.fields_for :question_answers, question.question_answers.build do |a| %>
<%= f.label question.question %>
<%= a.text_area :answer, class: 'form-control' %>
<%= a.hidden_field :survey_response_id, value: @survey.id %>
<% end %>
<% end %>
</div>
<% end %>
<div><%= f.submit "Submit", :class => "btn btn-primary" %></div>
<% end %>
</div>
我的表单呈现正确,但是当我提交它时,我的params哈希看起来像这样:
{"utf8"=>"✓",
"authenticity_token"=>"p8Tky2So10TH4FUUvLKhIh7j2vjNN39b3HDsF0cYE14=",
"survey_response"=>{"mini_post_questions"=>{"question_answers"=>{"answer"=>"", "survey_response_id"=>"11"}}},
"commit"=>"Submit",
"key"=>"e4fab42244db2286d471082696",
"controller"=>"surveys",
"action"=>"create"}
我希望每个问题都有一个mini_post_question
键,但我不确定如何达到这一点。
答案 0 :(得分:1)
我认为您走在正确的轨道上,但您需要让模型接受表单中所需对象的嵌套属性。
所以我认为在你的SurveyResponse
模型中,你会有
class SurveyResponse < ActiveRecord::Base
belongs_to :survey_invite
has_many :question_answers
accepts_nested_attributes_for :question_answers
has_many :mini_post_questions, through: :question_answers
end
此外,由于您似乎没有在这里修改迷你问题,我会说您不需要接受问题的嵌套属性,但只是有一个隐藏字段,将mini_question_id分配给question_answer
所以你的表格应该是这样的
<div class="form-box">
<%= form_for @response, url: "/survey?key=#{@invite.key}", method: :post, html: { role: 'form' } do |f| %>
<% @questions.each do |question| %>
<div class="form-group">
<%= f.fields_for :question_answers, @survey.question_answers.build do |q| %>
<%= q.label question.question %>
<%= q.text_area :answer, class: 'form-control' %>
<%= q.hidden_field :mini_post_question_id, value: question.id %>
<% end %>
</div>
<% end %>
<div><%= f.submit "Submit", :class => "btn btn-primary" %></div>
<% end %>
</div>
由于您的表单接受了question_answers的嵌套属性,因此您无需提供survey_id。
这可以让您更接近表格中所需的内容。