I created a model called exam_questions
(I didn't create a specific controller for it) and I tried to create multiple records at once with:
form:
<%= form_tag({:controller => "basic_methods", :action => "create_exam_questions"}, method: :post) do %>
<% (1..exam.number_of_questions).each do |question_number| %>
<div class="panel panel-default panel-question">
<div clas="panel-header" style="background-color: white;color:black;padding:15px;">
<h2>Question <%= question_number %>:</h2>
</div>
<div class="panel-body" style="padding:25px;">
<%= hidden_field_tag :exam_question_no, question_number, :name => "exam_questions[][question_no]" %>
<%= hidden_field_tag :exam_question_type, 1, :name => "exam_questions[][question_type]" %>
<%= hidden_field_tag :exam_question_exam_id, exam.id, :name => "exam_questions[][exam_id]" %>
<%= text_area_tag :exam_question_text, '', :placeholder => "Question....", class:"form-control", :name => "exam_questions[][question]", :rows => 10%>
<br />
<br />
<h3>Options seperated by a comma (e.g Mycobacterium Bovis,Mycobacterium Tuberclosis,Plasmodium Falciparum,Plasmodium vivax):</h3>
<%= text_field_tag :exam_question_exam_options, '', class:"form-control", :placeholder => "Options seperated by a comma e.g Mycobacterium Bovis,Mycobacterium Tuberclosis,Plasmodium Falciparum,Plasmodium vivax", :name => "exam_questions[][exam_options]"%>
<br />
<h3>Correct answers (option seperated by comma e.g A,B,C,D) or (single option e.g A) :</h3>
<%= text_field_tag :exam_question_correct_answers, '', class:"form-control", :placeholder => "Correct answer option seperated by comma e.g A,B,C,D", :name => "exam_questions[][correct_answers]"%>
<br />
<br />
</div>
</div>
<% end %>
<div class="text-center">
<%= submit_tag "Create Exam", class:"btn btn-primary" %>
</div>
<% end %>
controller(basic_methods_controller) method:
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, :exam_options, :question_type, :exam_id))
@exam_question.save
end
console:
Started POST "/create_exam_questions" for *******(#IP hidden by me) at 2018-07-07 09:54:21 +0000
Cannot render console from *******(#IP hidden by me)! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
/home/ubuntu/workspace/app/controllers/basic_methods_controller.rb:358: warning: key :name is duplicated and overwritten on line 358
Processing by BasicMethodsController#create_exam_questions as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"jTlqIpR8JeoX0quDSvmU+aCaT8aw3wGgopCczTo8zkQQrNX55TXj0IJsHtkDK0SISQNEqRj08Ekk6o4SR1pu3A==", "exam_questions"=>[{"question_no"=>"1", "question_type"=>"1", "exam_id"=>"7", "question"=>"If 5x plus 32 equals 4 minus 2x what is the value of x ?", "exam_options"=>"-4,-3,4,17,12", "correct_answers"=>"A"}, {"question_no"=>"2", "question_type"=>"1", "exam_id"=>"7", "question"=>"Which of the following numbers is farthest from the number 1 on the number line?", "exam_options"=>"-10,-5,0,5,10", "correct_answers"=>"A"}], "commit"=>"Create Exam"}
Teacher Load (0.4ms) SELECT "teachers".* FROM "teachers" WHERE "teachers"."id" = ? ORDER BY "teachers"."id" ASC LIMIT 1 [["id", 3]]
(0.2ms) begin transaction
SQL (0.8ms) INSERT INTO "exam_questions" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2018-07-07 09:54:21.732397"], ["updated_at", "2018-07-07 09:54:21.732397"]]
(19.1ms) commit transaction
(0.1ms) begin transaction
SQL (0.4ms) INSERT INTO "exam_questions" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2018-07-07 09:54:21.761920"], ["updated_at", "2018-07-07 09:54:21.761920"]]
(25.4ms) commit transaction
Redirected to *******(#(root_path) hidden by me)
Completed 302 Found in 83ms (ActiveRecord: 47.3ms)
but it always creates records with all attributes set to nil What am I doing wrong?