Rails:为什么是“number_with_delimiter”?在我的模型中无法识别的方法? [英] Rails: Why is the "number_with_delimiter" method not recognized inside my model?
本文介绍了Rails:为什么是“number_with_delimiter”?在我的模型中无法识别的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的验证:
class Product < ActiveRecord::Base
# include ActionView::Helpers::NumberHelper
...
validates_numericality_of :price, :less_than => 1000000,
:message => "must be less than #{number_with_delimiter(1000000)}"
...
end
在此代码中,我收到以下错误:
On this code, I have received the following error:
undefined method `number_with_delimiter' for #<Class:0x2665a58>
我试图添加:
include ActionView::Helpers::NumberHelper
但它没有帮助。
我缺少什么?
推荐答案
真正的问题这里是你将这个模块包含在类中,而不是用它扩展类。
The true problem here is that you're including this module into the class, rather than extended the class with it.
包含的差异将使方法在实例上可用,扩展的位置将使它们成为你尝试使用它们的地方:在课堂上。
The differences is an include will make the methods available on the instance, where as the extend will make them where you're trying to use them: on the class.
这篇关于Rails:为什么是“number_with_delimiter”?在我的模型中无法识别的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文