Rails:找不到 ID 为的 #controller
提交答案时,我收到此错误:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最好的猜测是您没有正确设置路线。假设您使用的是 Rails 3 并且正在使用资源,则需要添加以下内容:
这将创建一个类似
/questions/#{id}/answer
的路由。Answer 不是 HTTP 动词,因此在路由中使用
resources
不会创建到answer
操作的路由。根据评论进行编辑:
首先,如果您要更新或创建数据,则应使用
put
或post
。使用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:
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 youranswer
action.Edit based on comment:
First, if you're updating or creating data, you should use
put
orpost
. It's a bad idea to modify data on the server with aget
. 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 haveparams[:id]
. You won't getparams[:id]
if you try to do an action on a collection rather than a member.