问题描述
当提交答案时,我收到此错误:
ActiveRecord :: RecordNotFound ):
app / controllers / questions_controller.rb:6:in`show'
我理解我或者通过传递一个参数从窗体或
didn`t在我的控制器中正确定义错误。
感谢一些帮助找到
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)
如果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
$ b b
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 ::
发生当我尝试选择一个选择&
在Thu 12月01日开始为127.0.0.1 GET/ questions / 1 :38:36 -0500 2011
由QuestionsController处理#show as
参数:{id=>1}
SQL(0.6ms)SELECT name
FROM sqlite_master
WHERE type ='table'AND NOT name ='sqlite_sequence'
问题载入(0.3ms)SELECTquestions。* FROMquestionsWHEREquestions。id= 1 LIMIT 1
选择加载(10.8ms)SELECTchoices。* FROMchoicesWHERE(choices.question_id = 1)
在布局/应用程序
完成200 OK在424ms(视图:118.0ms | ActiveRecord:11.6ms)
在Thu的127.0.0.1开始GET/ questions / answer?id = 1 Dec 01 01:38:38 -0500 2011
处理QuestionController#show as
参数:{id=>answer}
问题负载。* FROMquestionsWHEREquestions。id= 0 LIMIT 1
在10ms内完成
ActiveRecord :: RecordNotFound(无法找到ID = answer的问题):
app / controllers / questions_controller.rb:6:in`show'
。
我最好的猜测是,你没有正确设置路线。假设您使用的是Rails 3,而您正在使用资源,则需要添加以下内容:
resources:questions do
member do
put'answer'
end
end
$ b b
这将创建一个路径,例如 / questions /#{id} / answer
。
答案不是HTTP动词,因此在路线中使用资源
不会创建到 answer
动作。
根据评论进行编辑:
,如果您要更新或创建数据,则应使用 put
或 post
。用 get
修改服务器上的数据是一个坏主意。其次,我假设你将对每个问题做一个答案。如果是这样,您应该对成员执行操作,而不是集合。此外,在您的回答操作中,您还有 params [:id]
。如果您尝试对集合而不是成员执行操作,则不会获得 params [:id]
。
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 ordidn`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.
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.
这篇关于Rails:无法找到ID为的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!