这是我的源代码

 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'

我对如何调试这一点,我不明白怎么能这样错误筹集不知道。

任何帮助真正的赞赏。

由于

有帮助吗?

解决方案

如果您不响应非HTML客户端,您不必使用respond_to代码。

尝试改变的方法:

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

如果这样的作品,错误看起来是在您的应用程序的MIME类型配置的地方。

其他提示

当你不使用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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top