Ich habe eine quiz
welche has_many
questions
welche has_many
answers
. Antworten belongs_to
Fragen, die belongs_to
ein Quiz.verschachtelt erlaubt mir Probleme geben
Ich stoße auf ein Problem, wo, wenn ich versuche, eines meiner Quiz zu aktualisieren, egal was ich tue, nachdem ich das Quiz die Anzahl der Fragen (und die Antworten innerhalb jeder Frage) verdoppelt. Dies geschieht, wenn ich versuchte entsprechende Genehmigungen zu tun:
params.require(:quiz).permit(:name, questions_attributes: [:content, :explanation, :passage, answers_attributes: [:content, :correct_answer]])
Wenn ich mir nur params.require(:quiz).permit!
nicht tun, um dieses Problem zu bekommen. Warum ist das der Fall? Ich habe das Formular unten enthalten:
<%= 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 %>
hm. Ich änderte die Erlaubnis zu "params.require (: quiz) .permit (: Name, questions_attributes: [: Inhalt,: quiz_id,: Erklärung,: Passage, Antworten_Attributes: [: Inhalt,: question_id,: correct_answer]])' aber Das Problem besteht immer noch. –
Sie müssen zulassen: ID-Feld wie folgt: https://gist.github.com/ad795fa119044678fb504a5694a7ef32 – iosadchii