Rails:找不到 ID 为的 #controller

发布于 2024-12-19 02:40:15 字数 2559 浏览 1 评论 0原文

提交答案时,我收到此错误:

ActiveRecord::RecordNotFound (Couldn't find Question with ID=answer):
  app/controllers/questions_controller.rb:6:in `show'

据我所知,我要么在从表单传递参数时犯了错误,要么 在我的控制器中没有正确定义它。

希望能帮助您找到此错误,提前致谢!

Questions_Controller:

class QuestionsController < ApplicationController
  def index
  end

  def show
      @question = Question.find(params[:id])
      @choices = @question.choices
  end

  def answer
      @choice = Choice.find(:first, :conditions => { :id => params[:id] })
      @answer = Answer.create(:question_id => @choice.question_id, :choice_id => @choice.id)

      if Question.last == @choice.question
        render :action => "thank_you"
      else
        question = Question.find(:first, :conditions => { :position => (@choice.question.position + 1) })
        redirect_to question_path(:id => question.id)
      end
   end
end

views/questions/show.html.erb:::

<div data-role="content">
  <div align="center">
    <h3><%= @question.question %></h3>
  </div>
  <br><br>
  <ul data-role="listview">
    <% @choices.each_with_index do |c, i| %>
      <% i = i + 1 %>
      <li data-theme="c">
        <%= link_to "#{i}. #{c.choice}", answer_questions_path(:id => c.id) %>
      </li>
    <% end %>
  </ul>
</div>

EDIT::

当我尝试选择一个选项时会发生这种情况在回答第一个问题时提交答案。

Started GET "/questions/1" for 127.0.0.1 at Thu Dec 01 01:38:36 -0500 2011
  Processing by QuestionsController#show as 
  Parameters: {"id"=>"1"}
  SQL (0.6ms)   SELECT name
 FROM sqlite_master
 WHERE type = 'table' AND NOT name = 'sqlite_sequence'
  Question Load (0.3ms)  SELECT "questions".* FROM "questions" WHERE "questions"."id" = 1 LIMIT 1
  Choice Load (10.8ms)  SELECT "choices".* FROM "choices" WHERE ("choices".question_id = 1)
Rendered questions/show.html.erb within layouts/application (28.8ms)
Completed 200 OK in 424ms (Views: 118.0ms | ActiveRecord: 11.6ms)


Started GET "/questions/answer?id=1" for 127.0.0.1 at Thu Dec 01 01:38:38 -0500 2011
  Processing by QuestionsController#show as 
  Parameters: {"id"=>"answer"}
  Question Load (0.1ms)  SELECT "questions".* FROM "questions" WHERE "questions"."id" = 0 LIMIT 1
Completed   in 10ms

ActiveRecord::RecordNotFound (Couldn't find Question with ID=answer):
  app/controllers/questions_controller.rb:6:in `show'

希望这有帮助。

When submitting an answer I get this error:

ActiveRecord::RecordNotFound (Couldn't find Question with ID=answer):
  app/controllers/questions_controller.rb:6:in `show'

From what I understand I either made a error with passing an argument from the form or
didn`t define it correctly in my controller.

Would appreciate some help finding this bug, thanks in advance!

Questions_Controller:

class QuestionsController < ApplicationController
  def index
  end

  def show
      @question = Question.find(params[:id])
      @choices = @question.choices
  end

  def answer
      @choice = Choice.find(:first, :conditions => { :id => params[:id] })
      @answer = Answer.create(:question_id => @choice.question_id, :choice_id => @choice.id)

      if Question.last == @choice.question
        render :action => "thank_you"
      else
        question = Question.find(:first, :conditions => { :position => (@choice.question.position + 1) })
        redirect_to question_path(:id => question.id)
      end
   end
end

views/questions/show.html.erb :

<div data-role="content">
  <div align="center">
    <h3><%= @question.question %></h3>
  </div>
  <br><br>
  <ul data-role="listview">
    <% @choices.each_with_index do |c, i| %>
      <% i = i + 1 %>
      <li data-theme="c">
        <%= link_to "#{i}. #{c.choice}", answer_questions_path(:id => c.id) %>
      </li>
    <% end %>
  </ul>
</div>

::EDIT::

This happens when I try to select a choice & submit an answer while on the first question.

Started GET "/questions/1" for 127.0.0.1 at Thu Dec 01 01:38:36 -0500 2011
  Processing by QuestionsController#show as 
  Parameters: {"id"=>"1"}
  SQL (0.6ms)   SELECT name
 FROM sqlite_master
 WHERE type = 'table' AND NOT name = 'sqlite_sequence'
  Question Load (0.3ms)  SELECT "questions".* FROM "questions" WHERE "questions"."id" = 1 LIMIT 1
  Choice Load (10.8ms)  SELECT "choices".* FROM "choices" WHERE ("choices".question_id = 1)
Rendered questions/show.html.erb within layouts/application (28.8ms)
Completed 200 OK in 424ms (Views: 118.0ms | ActiveRecord: 11.6ms)


Started GET "/questions/answer?id=1" for 127.0.0.1 at Thu Dec 01 01:38:38 -0500 2011
  Processing by QuestionsController#show as 
  Parameters: {"id"=>"answer"}
  Question Load (0.1ms)  SELECT "questions".* FROM "questions" WHERE "questions"."id" = 0 LIMIT 1
Completed   in 10ms

ActiveRecord::RecordNotFound (Couldn't find Question with ID=answer):
  app/controllers/questions_controller.rb:6:in `show'

Hope this helps.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天煞孤星 2024-12-26 02:40:15

我最好的猜测是您没有正确设置路线。假设您使用的是 Rails 3 并且正在使用资源,则需要添加以下内容:

resources :questions do 
  member do
     put 'answer'
  end
end

这将创建一个类似 /questions/#{id}/answer 的路由。

Answer 不是 HTTP 动词,因此在路由中使用 resources 不会创建到 answer 操作的路由。

根据评论进行编辑:

首先,如果您要更新或创建数据,则应使用 putpost。使用 get 修改服务器上的数据是一个坏主意。其次,我假设您会回答每个问题。如果是这种情况,您应该对成员而不是集合执行该操作。此外,在您的应答操作中,您有 params[:id]。如果您尝试对集合而不是成员执行操作,您将不会获得 params[:id]

My best guess is that you don't have a route correctly setup. Assuming that you're using Rails 3 and you're using resources, you need to do add the following:

resources :questions do 
  member do
     put 'answer'
  end
end

This will create a route like /questions/#{id}/answer.

Answer is not an HTTP verb, so using resources in your routes will not create a route to your answer action.

Edit based on comment:

First, if you're updating or creating data, you should use put or post. It's a bad idea to modify data on the server with a get. Secondly, I assume that you would be doing an answer per question. If that is the case, you should do the action on a member, not a collection. Also, in your answer action, you have params[:id]. You won't get params[:id] if you try to do an action on a collection rather than a member.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文