2010-01-09 57 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