Rails fields_for 参数空白
Rails fields_for params blank
我正在尝试为调查构建一个嵌套表单,但我的参数散列在 fields_for
块中缺少值时遇到问题。
我有以下型号。该应用程序的工作方式是管理员创建 MiniPost
及其 MiniPostQuestions
。然后客户端使用 MiniPosts
之一创建一个新的 MiniPostSurvey
。客户端然后通过电子邮件发送 SurveyInvites
给用户。当用户响应时,将创建一个新的 SurveyResponse
,并为调查中的每个 MiniPostQuestion
创建一个新的 QuestionAnswer
。
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>
我的表单正确呈现,但是当我提交它时,我的参数哈希看起来像这样:
{"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
键,但我不确定如何做到这一点。
我认为您的方向是正确的,但您需要让您的模型接受表单中所需对象的嵌套属性。
所以我认为在您的 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_answer 的嵌套属性,因此您无需提供 survey_id。
这应该让您更接近您在表单中需要的内容。
我正在尝试为调查构建一个嵌套表单,但我的参数散列在 fields_for
块中缺少值时遇到问题。
我有以下型号。该应用程序的工作方式是管理员创建 MiniPost
及其 MiniPostQuestions
。然后客户端使用 MiniPosts
之一创建一个新的 MiniPostSurvey
。客户端然后通过电子邮件发送 SurveyInvites
给用户。当用户响应时,将创建一个新的 SurveyResponse
,并为调查中的每个 MiniPostQuestion
创建一个新的 QuestionAnswer
。
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>
我的表单正确呈现,但是当我提交它时,我的参数哈希看起来像这样:
{"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
键,但我不确定如何做到这一点。
我认为您的方向是正确的,但您需要让您的模型接受表单中所需对象的嵌套属性。
所以我认为在您的 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_answer 的嵌套属性,因此您无需提供 survey_id。
这应该让您更接近您在表单中需要的内容。