嵌套许可给我带来麻烦

时间:2016-08-22 19:25:12

标签: ruby-on-rails

我有quiz has_many questions has_many answers。答案belongs_to belongs_to一个测验的问题。

我遇到了一个问题,如果我尝试更新我的一个测验,无论我做什么,在我提交测验后,问题的数量(以及每个问题中的答案)都会翻倍。当我试图做适当的许可时会发生这种情况:

params.require(:quiz).permit(:name, questions_attributes: [:content, :explanation, :passage, answers_attributes: [:content, :correct_answer]])

如果我这样做params.require(:quiz).permit!我就不会遇到这个问题。为什么会这样?我已经包含以下表格:

<%= form_for(@quiz) do |f| %>
  <% if @quiz.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@quiz.errors.count, "error") %> prohibited this quiz from being saved:</h2>

      <ul>
      <% @quiz.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
    <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>


    <%= f.fields_for :questions do |question_attribute| %>
    <div class = 'inner-c'>
   <p>
    <%= question_attribute.label :content, "Question" %>    <span><b><%= question_attribute.index + 1 %></b></span> <br/> 
    <%= question_attribute.text_area :content, :cols => 100, :rows => 4 %>
    </p>
    <p>
    <%= question_attribute.label :explanation, "Answer Explanation" %> <br/>
    <%= question_attribute.text_area :explanation, :cols => 100, :rows => 6 %>

</p>
    <%= question_attribute.label :_destroy, "Remove Question"%> 
    <%= question_attribute.check_box :_destroy %><br/>

    <%= question_attribute.label :passage, "Reference Passage" %> <br/>
    <%= question_attribute.text_area :passage, :rows => 3, :class => 'passage-input' %>


    <%#= question_attribute.label :question_explanation, "Question Explanation" %>
    <%#= question_attribute.text_area :question_explanation, :rows => 10 %>


  </p>

   <%= question_attribute.fields_for :answers do |answer_attribute| %>
    <p>
      <%= answer_attribute.label :content, "Answer" %>
      <%= answer_attribute.text_field :content %>
      <%= answer_attribute.label :correct_answer, "Check to indicate correct answer", :class => 'inline' %>
      <%= answer_attribute.check_box :correct_answer, :class => 'inline'%>

    </p>
   <% end %> 

   </div> <!-- inner-c -->
 <% end %> 


  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

1 个答案:

答案 0 :(得分:0)

最有可能的是,创建了新记录,因为ID被过滤掉了。尝试允许嵌套模型的id列