如何在rails中的模块中包含validates_confrmation_of

时间:2009-05-14 15:22:38

标签: ruby-on-rails

我想加入

validates_confirmation_of :password

在一个模块中,但我不断收到如下错误:

“密码:: ClassMethods的未定义方法`validates_confirmation_of”:模块“

不确定如何让它发挥作用。

感谢

1 个答案:

答案 0 :(得分:3)

您无法在模块中调用validates_confirmation_of,因为模块的代码在创建时会执行。相反,您希望在模块包含在ActiveRecord模型中时调用验证方法,如下所示:

module Password::ClassMethods
  def self.included(base)
    base.send(:validates_confirmation_of, :password)
  end
end