Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在更新操作中,将抛出一个NoMethodError_Ruby On Rails_Actionpack - Fatal编程技术网

Ruby on rails 在更新操作中,将抛出一个NoMethodError

Ruby on rails 在更新操作中,将抛出一个NoMethodError,ruby-on-rails,actionpack,Ruby On Rails,Actionpack,这是我的源代码 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 |fo

这是我的源代码

 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对象。错误发生在计算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客户端,则不必使用“响应”按钮

尝试将方法更改为:

  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

谢谢托比。这很有帮助,现在我必须检查我的mime类型配置。