简体   繁体   English

Railsfields_for参数为空

[英]Rails fields_for params blank

I'm trying to build a nested form for a survey and having problems with my params hash missing values from within the fields_for block. 我正在尝试为调查构建一个嵌套表格,并且我的参数存在问题,因为fields_for无法在fields_for块中散列缺少的值。

I have the following models. 我有以下型号。 The way the app works is that an admin creates a MiniPost and its MiniPostQuestions . 该应用程序的工作方式是管理员创建一个MiniPost及其MiniPostQuestions Then a client creates a new MiniPostSurvey using one of the MiniPosts . 然后,客户端使用MiniPostSurvey之一创建一个新的MiniPosts The client then emails out SurveyInvites to users. 然后,客户端通过电子邮件将SurveyInvites发送给用户。 When a user responds, a new SurveyResponse is created, along with a new QuestionAnswer for each MiniPostQuestion in the survey. 当用户做出响应时, SurveyResponse QuestionAnswer中的每个MiniPostQuestion创建新的SurveyResponse以及新的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

Here is my response form. 这是我的回复表格。

<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>

My form renders correctly, however when I submit it, my params hash looks like this: 我的表单可以正确呈现,但是当我提交表单时,我的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"}

I would expect a mini_post_question key for each question, but I'm not sure how to get to that point. 我希望每个问题都有一个mini_post_question键,但是我不确定如何达到这一点。

I think you are on the right track, but you need to have your models accept nested attributes for the objects that you want in your form. 我认为您走在正确的道路上,但是您需要让模型接受表单中所需对象的嵌套属性。

So I think in your SurveyResponse model, you would have 所以我认为在您的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

Also, since it looks like you are not modifying the mini question here, I would say that you don't need to accept nested attributes for the questions, but just have a hidden field that assigns the mini_question_id to the question_answer 另外,由于您似乎没有在此处修改迷你问题,因此我想说您不需要接受嵌套的问题属性,而只需要一个隐藏字段即可将mini_question_id分配给question_answer

So your form should be something like this 所以你的表格应该是这样的

<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>

Since your form is accepting nested attributes for question_answers, you won't need to supply the survey_id. 由于您的表单接受question_answer的嵌套属性,因此您无需提供survey_id。

This should get you closer to what you would need in your form. 这应该使您更接近表单中的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM