ruby-on-rails - 创建对象时传递参数没有控制器问题的模型
问题描述
我创建了一个名为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 %>
控制器(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 的记录我做错了什么?
解决方案
我在我的虚拟应用程序中测试了你的确切代码,它可以工作!查看控制台输出:
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!)
.
从控制台消息中,也可以尝试:
- 将 IP 列入白名单: 如何在 Rails 上禁用“无法从...渲染控制台”
- 控制器线 358:
basic_methods_controller.rb:358: warning: key :name is duplicated and overwritten on line 358
- 师范:
Teacher Load (0.4ms)...
交易开始前,这是什么?
推荐阅读
- sql - 雪花创建表语法
- npm - Sencha Charts 是否从 ExtJS 7.0 Classic GPL 中删除?
- angular - 在数组上使用地图函数 - 如何仅转换一个字段(MEAN App)
- r - 在 R 中使用 Ordinal ModV 运行调节中介分析
- javascript - 将输入框的值与 JavaScript 中的先前结果相除
- c# - 从 C# 中的 json 获取值
- typescript - 混入中的打字稿棘手语法
- c# - ANTLR 匹配字符串中的日期时间
- javascript - 如何使用 webpack 初始化 bootstrap 可关闭警报
- scala - 生成无副作用的随机数