查询返回Rails控制台中的记录,但模型导致500内部服务器错误

时间:2016-09-17 06:46:54

标签: ruby-on-rails

上下文 - 我正在创建一个测验应用来学习rails。特定视图显示问题列表和单选按钮。用户必须为每个问题选择选项,然后单击“提交”按钮。 “提交”按钮调用AJAX调用并调用控制器。

问题 - 在Controller中,查询返回

  

7ms完成500内部服务器错误(ActiveRecord:0.2ms)

但是当我在Rails控制台中运行相同的查询时,我得到了正确的结果。

日志输出 -

  

问题加载(0.2ms)SELECT“questions”。* FROM“questions”WHERE“questions”。“id”=?限制1 [[“id”,29]]

     

7ms完成500内部服务器错误(ActiveRecord:0.2ms)

Rails控制台输出 -

  

问题加载(0.6ms)SELECT“questions”。* FROM“questions”WHERE“questions”。“id”=?限制1 [[“id”,29]]   => #   IRB(主):017:0>

模型

class Question < ActiveRecord::Base
  belongs_to :title
end

表架构

create_table "questions", force: :cascade do |t|
  t.string   "question"
  t.string   "option1"
  t.string   "option2"
  t.string   "option3"
  t.string   "option4"
  t.string   "answer"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.integer  "title_id"
end

控制器

  def check_answers
    answers = params[:answers]
    @response = []
    my_hash = {}
    answers.each_key do |key|
      question_id = answers[key]['question_id']
      user_answer = answers[key]['user_answer']
      correct_answer = Question.find_by id: question_id
      correct_answer_jk = correct_answer[0]['answer']
      my_hash['question_id'] = question_id
      my_hash['user_answer'] = user_answer
      if user_answer == correct_answer_jk
        my_hash['response'] = 'Correct'
        my_hash['correct_answer'] = ''
      else
        my_hash['response'] = 'Incorrect'
        my_hash['correct_asnwer'] = correct_answer_jk
      end
      @response.push(my_hash)
    end
    respond_to do |format|
      format.js
      format.html
    end
  end

我已经搜索了这个问题,但我没有得到解决问题的任何指示。请注意我使用的是Sqlite。

已编辑 development.log

  

开始GET   ?“/ questions_controller / check_answers答案%5B0%5D%5Bquestion_id%5D = 29&安培;答案%5B0%5D%5Buser_answer%5D = B和答案%5B1%5D%5Bquestion_id%5D = 30&安培;答案%5B1%5D%5Buser_answer% 5D = A和答案%5B2%5D%5Bquestion_id%5D = 31&安培;答案%5B2%5D%5Buser_answer%5D = d&安培;答案%5B3%5D%5Bquestion_id%5D = 32&安培;答案%5B3%5D%5Buser_answer%5D = C&安培;答案%5B4%5D%5Bquestion_id%5D = 33&安培;答案%5B4%5D%5Buser_answer%5D = B和答案%5B5%5D%5Bquestion_id%5D = 34&安培;答案%5B5%5D%5Buser_answer%5D = A和答案%5B6%5D%5Bquestion_id%5D = 35&安培;答案%5B6%5D%5Buser_answer%5D = B”   for :: 1 at 2016-09-18 01:03:15 +0530由...处理   QuestionsController#check_answers为 / 参数:   {“answers”=&gt; {“0”=&gt; {“question_id”=&gt;“29”,“user_answer”=&gt;“B”},   “1”=&gt; {“question_id”=&gt;“30”,“user_answer”=&gt;“A”},   “2”=&gt; {“question_id”=&gt;“31”,“user_answer”=&gt;“D”},   “3”=&gt; {“question_id”=&gt;“32”,“user_answer”=&gt;“C”},   “4”=&gt; {“question_id”=&gt;“33”,“user_answer”=&gt;“B”},   “5”=&gt; {“question_id”=&gt;“34”,“user_answer”=&gt;“A”},   “6”=&gt; {“question_id”=&gt;“35”,“user_answer”=&gt;“B”}}}传入的数据是   {“0”=&gt; {“question_id”=&gt;“29”,“user_answer”=&gt;“B”},   “1”=&gt; {“question_id”=&gt;“30”,“user_answer”=&gt;“A”},   “2”=&gt; {“question_id”=&gt;“31”,“user_answer”=&gt;“D”},   “3”=&gt; {“question_id”=&gt;“32”,“user_answer”=&gt;“C”},   “4”=&gt; {“question_id”=&gt;“33”,“user_answer”=&gt;“B”},   “5”=&gt; {“question_id”=&gt;“34”,“user_answer”=&gt;“A”},   “6”=&gt; {“question_id”=&gt;“35”,“user_answer”=&gt;“B”}}键为0
  [1m [36mQuestion Load(35.2ms)[0m [1mSELECT“问题”。* FROM   “问题”在哪里“问题”。“id”=?限制1 [0m [[“id”,29]]   在43毫秒内完成500内部服务器错误(ActiveRecord:35.2ms)

     

NameError(check_answers中未定义的局部变量或方法correct_answer_rels' for #<QuestionsController:0x007ff3d50dd3a8>):
app/controllers/questions_controller.rb:27:in
块'   app / controllers / questions_controller.rb:21:in each_key'
app/controllers/questions_controller.rb:21:in
check_answers'

     

渲染   /Users/tusharsaurabh/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_source.erb   (3.7ms)呈现   /Users/tusharsaurabh/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb(0.8ms )渲染   /Users/tusharsaurabh/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb   (2.1ms)呈现   /Users/tusharsaurabh/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb   (89.5ms)

模板

check_answers.js.erb

<% @response.each do |my_hash| %>
<% if my_hash['response'] == 'Correct' %>
    $('#<%=my_hash["question_id"]%>').css('background-color','green');
<%else%>
    $('#<%=my_hash["question_id"]%>').css('background-color','red');
<%end%>
<%end%>

4 个答案:

答案 0 :(得分:1)

对于您制作的每个可能是js / html的请求,rails会根据您的请求类型在app / views / controller_name目录中检查视图模板为action_name.html.erb / action_name.js.erb。

所以这里通过添加&#34; check_answers.js.erb&#34;进行检查。文件在各自的目录。并在那里指定你的JavaScript。

或者,如果您不想指定视图模板,则应指定在respond_to块处呈现的内容      format.js {render json:{your_json}}

下面的部分与您的问题无关,但提供代码的优化和重构代码段以及一些小的更正:

的变化:   1)使用each_value而不是each_key进行哈希处理,因为您只处理值。   2)DB查询移动到模型。   3)改变了my_hash的创建方式。   4)减少if-else循环到if

@response =[]
answers.each_value do |value|
    correct_answer = Question.get_saved_answer(value['question_id'])
    my_hash = {'question_id':  value['question_id'], 'user_answer': value['user_answer'], 'response': 'Correct'}
    if user_answer != correct_answer
        my_hash['response'] = 'Incorrect'
        my_hash['correct_asnwer'] = correct_answer
    end
    @response.push(my_hash)
end


Question.rb

def get_saved_answer(question_id)
    result = Question.find_by id: question_id
    result[0]['answer']
end

希望在您继续学习rails时以某种方式帮助您。

答案 1 :(得分:0)

Rails正在尝试在Ajax请求上呈现模板。如果你没有指定要渲染的内容,它会认为你有一个要渲染的模板。如果你没有,你将得到500错误

format.js {render json: {foo: 'bar'}}

答案 2 :(得分:0)

Rails期待您的check_answers操作的视图模板。由于它在app/views/your_controller中找不到,因此它以500状态代码进行响应。您应该能够在服务器日志中看到错误跟踪。

要解决此问题,请添加JS视图模板,例如check_answers.js.erb目录中的app/views/your_controller,带有相应的JavaScript响应。

答案 3 :(得分:0)

问题是 - 我期待一个数组因此我编码 -

correct_answer = Question.find_by id: question_id
correct_answer_jk = correct_answer[0]['answer']

但是find_by不会返回数组,而是会返回一个Active Record :: Relation。刚刚删除[0]并且它有效。

感谢大家的时间和支持。