Ruby on rails 用于'的未定义方法;[]和#x27;for nil:Rails中的NilClass

Ruby on rails 用于'的未定义方法;[]和#x27;for nil:Rails中的NilClass,ruby-on-rails,Ruby On Rails,我试图用选项验证问题的答案。对于每个问题,我都有一个链接到检查答案的方法。问题是问题对象没有传递给方法。在同一页上,我有链接到传递问题对象的编辑方法。即使是链接到检查答案上的url也会显示正确的url。这里的错误在哪里 show.html.erb <p> <strong>Body:</strong> <%= @question.body %> </p> <p> <strong>Option:<

我试图用
选项验证
问题的答案。对于每个问题,我都有一个链接到
检查答案
的方法。问题是问题对象没有传递给方法。在同一页上,我有
链接到传递问题对象的
编辑
方法。即使是链接到
检查答案
上的url也会显示正确的url。这里的错误在哪里

show.html.erb

<p>
  <strong>Body:</strong>
  <%= @question.body %>
</p>

<p>
  <strong>Option:</strong>
  <% @question.options.each do |p| %>
    <%= radio_button_tag('option',p.id) %>
    <%= p.body %>

  <% end %>
</p>
<p><%= link_to 'Check Answer', check_answer_question_path(@question) %></p>
<p>
  <strong>User:</strong>
  <%= @question.user.email %>
</p>

<%= link_to 'Edit', edit_question_path(@question) %> |
<%= link_to 'Back', questions_path %>
def check_answer        
    puts(@question.id)        
end

private
    # Use callbacks to share common setup or constraints between actions.
    def set_question
      @question = Question.find(params[:id])
    end
Rails.application.routes.draw do
  resources :questions do
    member do
        get 'check_answer'
    end
  end
end
routes.rb

<p>
  <strong>Body:</strong>
  <%= @question.body %>
</p>

<p>
  <strong>Option:</strong>
  <% @question.options.each do |p| %>
    <%= radio_button_tag('option',p.id) %>
    <%= p.body %>

  <% end %>
</p>
<p><%= link_to 'Check Answer', check_answer_question_path(@question) %></p>
<p>
  <strong>User:</strong>
  <%= @question.user.email %>
</p>

<%= link_to 'Edit', edit_question_path(@question) %> |
<%= link_to 'Back', questions_path %>
def check_answer        
    puts(@question.id)        
end

private
    # Use callbacks to share common setup or constraints between actions.
    def set_question
      @question = Question.find(params[:id])
    end
Rails.application.routes.draw do
  resources :questions do
    member do
        get 'check_answer'
    end
  end
end
错误堆栈

Started GET "/questions/22/check_answer" for 127.0.0.1 at 2019-05-04 19:51:17 +0530
Processing by QuestionsController#check_answer as HTML
  Parameters: {"id"=>"22"}
  User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?  [["id", 1], ["LIMIT", 1]]
  ↳ /home/laxmanrm/.rvm/gems/ruby-2.6.0/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98
------------------------------------check_answer------------------------------------------
Completed 500 Internal Server Error in 5ms (ActiveRecord: 0.4ms)



NoMethodError (undefined method `id' for nil:NilClass):

app/controllers/questions_controller.rb:72:in `check_answer'
错误


在调用check\u-answer操作之前,您需要填充@question实例变量,以便在操作之前使用

before_action :set_question, only: [:show, :edit, :update, :destroy, :check_answer]

在调用check\u-answer操作之前,您需要填充@question实例变量,以便在check\u-answer操作之前使用

before_action :set_question, only: [:show, :edit, :update, :destroy, :check_answer]

该错误的确切来源是什么?方法
check\u-answer
没有获取
问题
对象。为了检查它,我添加了
函数放入打印
问题
对象的方法中,但我得到了
错误。请将您的错误堆栈添加到您的问题中好吗?我已经添加了
错误堆栈
。错误到底来自哪里?方法
检查答案
没有得到正确的答案
问题
对象。为了检查它,我添加了
函数放入打印
问题
对象的方法中,但我得到了
错误。您可以将错误堆栈添加到问题中吗?我已经添加了
错误堆栈
。我已经添加了它。我没有上传完整的控制器文件
before\u action:set\u question,仅限:[:show,:edit,:update,:destroy]before\u action:authenticate\u user!,除了:[:显示,:索引]
。如果需要,我可以添加它。我应该在
only
数组中添加
:check\u answer
方法吗?在这里,before\u操作仅适用于操作:show、:edit、:update、:destroy哦,好!非常感谢。我犯了太多愚蠢的错误,我已经加上了。我没有上传完整的控制器文件
before\u action:set\u question,仅限:[:show,:edit,:update,:destroy]before\u action:authenticate\u user!,除了:[:显示,:索引]
。如果需要,我可以添加它。我应该在
only
数组中添加
:check\u answer
方法吗?在这里,before\u操作仅适用于操作:show、:edit、:update、:destroy哦,好!非常感谢。我犯了太多愚蠢的错误。