ruby-on-rails – Rails 3,未知验证器:’EmailValidator’

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – Rails 3,未知验证器:’EmailValidator’前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。



我尝试在我的rails应用程序中添加一个电子邮件验证器.我创建了以下文件/lib/validators/email_validator.rb
class EmailValidator < ActiveModel::EachValidator
  def validate_each(object,attribute,value)  
    unless value =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i  
      object.errors[attribute] << (options[:message] || "is not formatted properly")  
    end  
  end  
end

在application.rb中,我添加了这一行:

config.autoload_paths<<< “#{config.root} / LIB /验证器” 这里是我的用户型号:

class User < ActiveRecord::Base
  attr_accessible :email,:password,:name
  validates :email,:presence => true,:uniqueness => true,:email => true  
end

如果我想启动服务器我有一个错误

Unknown validator: 'EmailValidator' (ArgumentError)

有人有一个想法如何解决这个问题?

解决方法

If you place your custom validators in app/validators they will be
automatically loaded without needing to alter your
config/application.rb file.

资源:Where should Rails 3 custom validators be stored?(第二个答案)

原文链接:https://www.f2er.com/ruby/273505.html

猜你在找的Ruby相关文章