Rails:找不到ID为的#controller
提交答案时,我收到此错误:
ActiveRecord::RecordNotFound (Couldn't find Question with ID=answer): app/controllers/questions_controller.rb:6:in `show'
根据我的理解,我要么从表单中传递参数,要么在我的控制器中没有正确定义它。
非常感谢一些帮助找到这个bug,提前谢谢!
Questions_Controller:
class QuestionsController { :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:
- c.id) %>
::编辑::
当我在第一个问题上尝试选择并提交答案时会发生这种情况。
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'
希望这可以帮助。
我最好的猜测是你没有正确设置路线。 假设您正在使用Rails 3并且您正在使用资源,则需要添加以下内容:
resources :questions do member do put 'answer' end end
这将创建一个像/questions/#{id}/answer
这样的路线。
答案不是HTTP动词,因此使用路线中的resources
不会创建到answer
操作的路径。
根据评论进行编辑:
首先,如果您要更新或创建数据,则应使用put
或post
。 使用get
修改服务器上的数据是个坏主意。 其次,我假设你会在每个问题上做一个答案。 如果是这种情况,您应该对成员而不是集合执行操作。 另外,在你的回答动作中,你有params[:id]
。 如果您尝试对集合而不是成员执行操作,则不会获得params[:id]
。