【问题标题】:Cannot submit form values correctly无法正确提交表单值
【发布时间】:2014-02-14 21:19:04
【问题描述】:

我有一个包含问题和答案的表格,它包含:student_idsurvey_idquestion_idanswer_id。我希望每个表格都应该保存与问题相等的记录。 我有两个问题:

1- 我的创建操作中只有第一个属性 (student_id) 保存为 0 value ,其他属性保存为 Null

2- 表单提交 2 条额外记录。当我回答 3 个问题时,我当前的表单有 3 个问题,我在数据库中得到了 5 条记录(第一条记录只有空值,student_id = 0 除外,其他记录所有属性都为空值。

控制器:

def create
#student_id saved as 0 and the rest attributes saved as Null
#always save two extra records with null value even when i answer all questions
    params[:subject_survey].each do |student, survey, question, answer|
        @sv_sub = SubjectSurvey.create(student_id: student["student_id"],
    survey_id: survey["survey_id"],
    question_id: question,
    answer_id: answer)
    end

    if @sv_sub.save
        redirect_to surveys_path, notice: "Success."
    else
        render :new, notice: "Failed."
    end
end

服务器日志:

#when answering three questions:

Started POST "/subject_surveys" for 127.0.0.1 at 2014-02-14 21:54:09 +0200
Processing by SubjectSurveysController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"C8w1p+tQCTY25SJU5TGJcqCqRoUw1TvVBjEJgpjPMPc=", "subject_survey"=>{"student_id"=>"1", "survey_id"=>"1", "2"=>{"answer_id"=>"7"}, "3"=>{"answer_id"=>"14"}, "4"=>{"answer_id"=>"19"}}}

#when answering one question:

Started POST "/subject_surveys" for 127.0.0.1 at 2014-02-14 21:50:29 +0200
Processing by SubjectSurveysController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"C8w1p+tQCTY25SJU5TGJcqCqRoUw1TvVBjEJgpjPMPc=", "subject_survey"=>{"student_id"=>"1", "survey_id"=>"1", "2"=>{"answer_id"=>"7"}}}

new.html.erb

<%= form_tag('/subject_surveys', method: :post, class: 'form-horizontal' ) do %>    
    <% q = 0 %>
    <% Survey.find(1).questions.order("questions.id asc").each do |question| %>
        <% q += 1%>
    <%= hidden_field_tag 'subject_survey[student_id]', "#{current_user.student.id}" %>
    <%= hidden_field_tag 'subject_survey[survey_id]', '1' %>
    <h6>Q<%= q %>- <%= question.content %></h6>
    <div class="clearfix"></div><div class="clearfix"></div>
    <% if question.question_type == "#{Question::CHECK}" %>
        <% question.answers.each do |answer| %>
            <%= check_box_tag "subject_survey[#{question.id}][answer_id][]", "#{answer.id}" %>
            <%= answer.content  %>
            <div class="clearfix"></div>
        <% end %>
    <% elsif question.question_type == "#{Question::RADIO}" %>
        <% question.answers.each do |answer| %>
            <%= radio_button_tag "subject_survey[#{question.id}][answer_id]", "#{answer.id}" %>
            <%= answer.content %>
        <div class="clearfix"></div>
        <% end %>
        <% end %>
    <% if question.id == Survey.find(1).questions.order("questions.id asc").last.id %>
        <br>
    <% else %>
        <hr style="border-top: 1px solid #000000;">
    <% end %>
    <% end %>
    <div class="form-actions no-margin">
        <%= submit_tag "Submit", class: "btn btn-info pull-right" %>
    <div class="clearfix"></div>
    </div>
<% end %>

【问题讨论】:

    标签: ruby-on-rails forms nested-forms


    【解决方案1】:

    我首先看到的一件事是您的控制器中不需要survey["survey_id"]student["student_id"]。你已经映射了它们,你只需要分别surveystudent

    另一方面,每个question 都有一个分配给它的答案哈希。所以,你应该这样对待。

    这意味着,在你的初始映射中

    params[:subject_survey].each do |student, survey, question, answer|
    

    最后一个参数(答案)没有位置。无论如何,这样做不是最正确的,所以看下面。

    另外,第三个参数 question 有点模糊......它不是作为“问题”传递的,而是作为指向一个值的数字。你应该把它变成一个数组。

    在你的表单中,改变这个:

    <%= check_box_tag "subject_survey[#{question.id}][answer_id][]", "#{answer.id}" %>
    

    进入这个(不需要告诉表单该值被标记为“answer_id”,你已经知道了,所以我们去掉了[answer_id] 部分)

    <%= check_box_tag "subject_survey[questions][#{question.id}]", "#{answer.id}" %>
    

    然后,在您的控制器操作中

    def create
        params[:subject_survey][:questions].each do |question, answer|
            @sv_sub = SubjectSurvey.create(
                student_id: params[:subject_survey][:student_id],
                survey_id: params[:subject_survey][:survey_id],
                question_id: question,
                answer_id: answer)
        end
        ....
    

    【讨论】:

    • 感谢您的回复,我收到了这个undefined method each' for nil:NilClass` for questions.each
    • 这就是参数`参数:{"utf8"=>"✓", "authenticity_token"=>"J5ZcUyKbnE7r3gxdeMiP7b7FVZ10Kh7BhUwaq5Mddew=", "subject_survey"=>{"student_id"=>"1", " survey_id"=>"1", "questions"=>{"2"=>{"answer_id"=>"7"}, "3"=>{"answer_id"=>"10"}}}, "提交"=>"提交"}`
    • 请注意,在表单中,[answer_id] 已被删除...不太确定您是否也应立即删除 []
    • 请注意,我还修改了无线电标签:&lt;%= radio_button_tag "subject_survey[questions][#{question.id}][answer_id]", "#{answer.id}" %&gt;
    • 你能粘贴新的错误,就像它显示的一样吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多