在更新操作中抛出 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 对象。评估 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您不响应非 html 客户端,则不必使用 respond_to。
尝试将方法更改为:
如果有效,则错误看起来像是在应用程序的 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 that works, the error looks like it is somewhere in the mime type configuration of your app.
当您不使用 yieled format 对象时,就会出现此神秘错误。事实上,当 update_attributes 调用失败时,您确实应该做一些事情,例如渲染 edit 模板:
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: