我嘗試在我的Rails應用程序中添加一個電子郵件驗證器。我創建以下文件/lib/validators/email_validator.rb
Rails 3,未知驗證器:'EmailValidator'
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/validators"
這裏是我的用戶模型:
class User < ActiveRecord::Base
attr_accessible :email, :password,:name
validates :email, :presence => true, :uniqueness => true, :email => true
end
,如果我想啓動服務器我得到一個錯誤:
Unknown validator: 'EmailValidator' (ArgumentError)
有沒有人有一個想法,我可以如何解決這個問題?
這不是問題的解決 – edikgat 2014-06-20 10:54:00