2011-12-01 111 views
1

ID當提交的答案,我得到這個錯誤:的Rails:Couldn`t找到#controller與

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

據我瞭解,我不是從形式或 didn`傳遞參數做了一個錯誤在我的控制器中正確定義它。

希望得到一些幫助找到這個bug,在此先感謝!

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 

的意見/問題/ 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> 

::編輯::

發生這種情況時,我嘗試選擇一個選項&提交第一個問題的答案。

Started GET "https://stackoverflow.com/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 "https://stackoverflow.com/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' 

希望這會有所幫助。

+0

請描述你如何得到這個錯誤信息。 –

+0

我發佈了一篇對原文的編輯,讓我知道是否還有其他我可以提供的內容。 –

回答

2

我最好的猜測是你沒有一個正確的路線設置。假設你正在使用的Rails 3和你使用的資源,你需要做的添加如下內容:

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

這將創建一個像/questions/#{id}/answer的路線。

答案不是HTTP動詞,因此在您的路線中使用resources不會創建通往您的answer操作的路線。

編輯基於評論:

首先,如果你要更新或創建數據,你應該使用putpost。用get來修改服務器上的數據是個不錯的主意。其次,我假設你會爲每個問題做一個回答。如果是這樣的話,你應該對會員而不是集合採取行動。此外,在你的回答行動中,你有params[:id]。如果您嘗試對集合而不是成員執行操作,則不會獲得params[:id]

+0

其實我把它定義爲:資源:問題做 收集做 得到:回答 結束 結束 –

+0

請看更新的答案。 –

+0

感謝您的回答,但是我不明白我將如何對會員而不是集合採取行動。我已切換到成員,但無法弄清楚如何修改視圖中的link_to方法以正常工作。 –