![](/img/trans.png)
[英]How to fix “ActionView::Template::Error” in Heroku/Ruby on Rails
[英]How to fix ActionView::Template::Error (Infinity) in Rails?
我在生产应用程序日志中注意到一个奇怪的错误:
ActionView::Template::Error (Infinity): /path/to/page/with/problem
/myapp/shared/bundled_gems/ruby/1.9.1/gems/activesupport-3.0.20/lib/active_support/core_ext/float/rounding.rb:16:in `round'
/myapp/shared/bundled_gems/ruby/1.9.1/gems/activesupport-3.0.20/lib/active_support/core_ext/float/rounding.rb:16:in `round'
/myapp/shared/bundled_gems/ruby/1.9.1/gems/activesupport-3.0.20/lib/active_support/core_ext/float/rounding.rb:14:in `round'
/myapp/current/path/to/file/with/problem.rb:106:in `my_buggy_method'
在代码中有这样的东西:
def my_buggy_method
(number1 - number2).round(2)
end
我没有任何关于触发此错误的数字的见解。 如何重构我的代码以避免此错误?
更新:我设法在生产中启动Rails控制台并跟踪上述方法中使用的值:
number1 == Infinity
number2 == 0
更新:经过进一步调查,number1因为除零而返回Infinity
:
number1 == 113 / 0.0
现在我知道这个Infinity
来自哪里,我会添加一个检查以确保我永远不会被零除。
导致此错误的原因是尝试round
或显示Infinity
。 我没想到这个号码,所以我添加了额外的检查以确保我不会通过除以零来创建Infinity
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.