所以我有一个基本上可以一次创建多个记录的表单。但是,我对(answer_mcq)字段有一些疑问:
<%= form_tag({:controller => "basic_methods", :action => "submit_answers"}, method: :post) do %>
<br />
<div class="text-center" id="button_container">
<%= submit_tag "Submit Answers", class:"btn btn-primary", :id => "submit_button1"%>
<%= submit_tag "Submit Answers", class:"btn btn-primary",:id => "submit_button" , :style =>"display:none" %>
</div>
<br />
<div id ="question_container">
<div id="question_carousel" class="carousel slide" data-ride="carousel" data-interval="false">
<div class="text-center">
<button class="btn btn-primary btn-lg" href="#question_carousel" role="button" data-slide="prev" style="position:relative;"><span class="glyphicon glyphicon-chevron-left"></span></button>
<button class="btn btn-primary btn-lg" href="#question_carousel" role="button" data-slide="next" style="position:relative;"><span class="glyphicon glyphicon-chevron-right"></span></button>
<br />
<br />
<button type="button" class="btn btn-primary" data-target="#navigate_to_question_modal" data-toggle="modal">Navigate to a specific question</button>
<br />
<br />
</div>
<div class="carousel-inner" role="listbox">
<div class="item active">
<div class="panel" style="border: solid 2px #8e44ad;padding-top:31%;padding-bottom:35%">
<div class="text-center">
<h1 style="color:black">Exam/Test: <%=@exam.name%></h1>
<h3 style="color:black">Click the left or right arrows to navigate between questions</h3>
</div>
</div>
</div>
<% @exam.exam_questions.each do |question|%>
<div class="item">
<div class="individual_question_container">
<div class="header">
<h3>Question: <%= question.question_no %> / <%= @exam.exam_questions.count %></h3>
</div>
<div class="container_body">
<br />
<h4><%= question.question%></h4>
<br />
</div>
<div class="footer">
<br />
<% if question.question_type == 1%>
<% if question.correct_answers.split(',').count == 1 %>
<% question.options.split(',').each do |option| %>
<%= radio_button_tag :exam_answer_answer_mcq, option_position_to_answer(question.options.split(',').find_index(option)), false, :name => "exam_answers[][answer_mcq_#{question.id}]"%> <!-- RADIO BUTTONS HERE -->
<label><%= option %></label>
<br />
<% end %>
<% else %>
<% question.options.split(',').each do |option| %>
<input type="checkbox" name ="<%=question.id%>--<%=question.options.split(',').find_index(option)%>--name" value="<%=option_position_to_answer(question.options.split(',').find_index(option))%>" id="<%=question.id%>--<%=question.options.split(',').find_index(option)%>--id" onclick="add_to_textbox('<%=question.id%>--<%=question.options.split(',').find_index(option)%>--id','<%=question.id%>--mainTextBox')">
<label><%= option %></label>
<br />
<% end %>
<%= hidden_field_tag :exam_answer_answer_mcq, '', :name => "exam_answers[][answer_mcq]", id:"#{question.id}--mainTextBox"%>
<% end %>
<% elsif question.question_type == 2 %>
<%= text_area_tag :exam_answer_answer_seq, '', :name => "exam_answers[][answer_seq]", :class => "form-control", :rows => 15, :placeholder => "Answer...."%>
<% end %>
<br />
</div>
</div>
</div>
<br />
<br />
<%= hidden_field_tag :exam_answer_exam_question_id, question.id, :name => "exam_answers[][exam_question_id]"%>
<%= hidden_field_tag :exam_answer_student_id, current_student.id, :name => "exam_answers[][student_id]"%>
<%= hidden_field_tag :exam_answer_exam_id, question.exam_id, :name => "exam_answers[][exam_id]"%>
<% end %>
</div>
</div>
</div>
<div id="final_message_container" style="display:none">
<h2 style="text-align:center">Time is up</h2>
</div>
<% end %>
因此,我尝试使用answer_mcq字段创建记录,但是单选按钮名称中的#{question.id}使创建该字段为零的记录变得有些困难。 控制器代码:
params[:exam_questions].each do |exam_question_params|
parameters = ActionController::Parameters.new(exam_question_params)
@exam_question = ExamQuestion.new(parameters.permit(:question, :correct_answers, :question_no, :options, :question_type, :exam_id))
@exam_question.save
end