我尝试在我的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.