我创建了一个名为exam_questions
的模型(我没有为其创建特定的控制器),并且尝试使用以下方法一次创建多个记录:
表格:
<%= 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)方法:
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
控制台:
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)
但是它总是创建所有属性都设置为nil的记录 我在做什么错了?
答案 0 :(得分:0)
您正在以一种非常规的方式设置参数。
我建议更改为更常规的内容:
注意: ActiveRecord的create
方法可以接受数组参数。这样做的好处是:有一个数据库事务。如果其中一条记录失败,那么它们都会全部失败。
例如:
# controller
def create
ExamQuestion.create(exam_question_params)
end
private
def exam_question_params
params.permit(exam_questions: [:question, :correct_answers, :question_no, :exam_options, :question_type, :exam_id])
end
答案 1 :(得分:0)
我在虚拟应用程序中测试了您的确切代码,它可以正常工作! 查看控制台输出:
Started POST "/create_exam_questions" for 127.0.0.1 at 2018-07-07 17:29:39 +0200
Processing by BasicMethodsController#create_exam_questions as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"p+i+8cvH642MbRgLb21CL8Y+yXmE51uKdKC7UgQg1VU=", "exam_questions"=>[{"question_no"=>"1", "question_type"=>"1", "exam_id"=>"4", "question"=>"Question text", "exam_options"=>"Exam options fill", "correct_answers"=>"C"}, {"question_no"=>"2", "question_type"=>"1", "exam_id"=>"4", "question"=>"Question text", "exam_options"=>"Exam options fill", "correct_answers"=>"C"}], "commit"=>"Create Exam"}
(0.1ms) begin transaction
SQL (0.4ms) INSERT INTO "exam_questions" ("correct_answers", "created_at", "exam_id", "exam_options", "question", "question_no", "question_type", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?) [["correct_answers", "C"], ["created_at", "2018-07-07 15:29:39.233939"], ["exam_id", 4], ["exam_options", "Exam options fill"], ["question", "Question text"], ["question_no", 1], ["question_type", 1], ["updated_at", "2018-07-07 15:29:39.233939"]]
(1.1ms) commit transaction
尝试使用@exam_question = ExamQuestion.new(parameters.permit!)
。
basic_methods_controller.rb:358: warning: key :name is duplicated and overwritten on line 358
Teacher Load (0.4ms)...
,这是什么意思?答案 2 :(得分:0)
好吧,我删除了整个模型,并运行了一个脚手架生成器,生成用于exam_questions的控制器和模型。 我使用了与问题中相同的代码,并且可以正常工作。我的发电机肯定有问题或有问题