2010-01-09 61 views
1

這是我的源代碼在更新行動NoMethodError被拋出

def update 
    @recipe = Recipe.find(params[:id]) 

    respond_to do |format| 
     if @recipe.update_attributes(params[:recipe]) 
     format.html {redirect_to :action => "edit" } 
     end 
    end 
    end 

我在這一行

respond_to do |format| 

和錯誤消息得到一個錯誤是:「你有一個零對象,當你沒想到它。評估nil.call時發生錯誤「。是

從堆棧跟蹤的五行如下

/Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/mime_responds.rb:175:in `respond' 
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/mime_responds.rb:173:in `each' 
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/mime_responds.rb:173:in `respond' 
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/mime_responds.rb:107:in `respond_to' 
/Path from my machine to the app/app/controllers/recipes_controller.rb:43:in `update' 

我對如何調試這個不知道,我不明白怎麼能這個錯誤得到提升。

任何幫助是真正的讚賞。

感謝

回答

0

如果您不響應非HTML客戶端,您不必使用respond_to代碼。

嘗試改變方法:

if @recipe.update_attributes(params[:recipe]) 
    redirect_to :action => "edit" 
    end 

如果這樣的作品,錯誤看起來是在您的應用程序的MIME類型配置的地方。

+0

感謝託比。它有幫助,現在我必須檢查我的MIME類型配置。 – iJK 2010-01-11 15:32:42

0

當您不使用yieled 格式對象時,會出現此隱蔽錯誤。事實上,你真正應該做的事情時,update_attributes方法調用失敗,例如渲染編輯模板:

def update 
    @recipe = Recipe.find(params[:id]) 

    respond_to do |format| 
     if @recipe.update_attributes(params[:recipe]) 
     format.html { redirect_to [:edit, @recipe] } 
     else 
     format.html { render :template => 'edit' } 
     end 
    end 
    end