【问题标题】:Model with no controller issue with passing parameters while creating object创建对象时传递参数没有控制器问题的模型
【发布时间】:2018-07-07 07:30:17
【问题描述】:

我创建了一个名为 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 的记录 我做错了什么?

【问题讨论】:

  • 您的代码似乎应该对我有用,您可以在 @exam_question.save 之前放置一个 raise Exception 并从控制台检查所有变量和对象,看看有什么问题。
  • 这里发生了很多事情,很难缩小问题的范围。请在提交表单时从终端发布日志输出...以Started POST 开头。请编辑您的原始问题,不要将其发布到 cmets。
  • @MarkMerritt 完成

标签: ruby-on-rails ruby ruby-on-rails-4 controller


【解决方案1】:

我在我的虚拟应用程序中测试了您的确切代码,它可以正常工作! 查看控制台输出:

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!)

从控制台消息中,也可以尝试:

【讨论】:

    【解决方案2】:

    您正在以一种非常规的方式设置参数。

    我建议改成更传统的东西:

    注意: 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
    

    【讨论】:

      【解决方案3】:

      好吧,我删除了整个模型并运行了一个脚手架生成器,为exam_questions 生成控制器和模型。 我使用了我在问题中发布的相同代码并且它有效。我的发电机一定有问题或什么的

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多