在更新操作中抛出 NoMethodError

发布于 2024-08-17 11:54:22 字数 973 浏览 5 评论 0原文

这是我的源代码

 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'

我不知道如何调试它,也不明白如何引发此错误。

非常感谢任何帮助。

谢谢

This is my source code

 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

I get an error on this line

respond_to do |format|

and the error message is "You have a nil object when you didn't expect it. The error occurred while evaluating nil.call".

The five lines from the stack trace are as follows

/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'

I have no idea on how to debug this and I cannot understand how can this error be raised.

Any help is truly appreciated.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

春风十里 2024-08-24 11:54:22

如果您不响应非 html 客户端,则不必使用 respond_to。

尝试将方法更改为:

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

如果有效,则错误看起来像是在应用程序的 mime 类型配置中的某个位置。

If you aren't responding to non-html clients, you don't have to use the respond_to.

Try changing the method to:

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

If that works, the error looks like it is somewhere in the mime type configuration of your app.

北渚 2024-08-24 11:54:22

当您不使用 yieled format 对象时,就会出现此神秘错误。事实上,当 update_attributes 调用失败时,您确实应该做一些事情,例如渲染 edit 模板:

  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

This cryptic error appears when you don't use the yieled format object. In fact, you really should do something when the update_attributes call fails, for example rendering the edit template:

  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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文