Unknown validator: 'email_format'
Rails.root: /home/saran/work_space/rails_apps/test_app
Application Trace | Framework Trace | Full Trace
app/models/user.rb:2
app/controllers/user_controller.rb:5:in `create'
我的用户模型文件如下: -
class User < ActiveRecord::Base
validates :email, :presence => true, :uniqueness => true, :email_format => true
end
我的lib类介绍如下:
:~/work_space/rails_apps/test_app/lib$ cat email_format_validator.rb
class EmailFormatValidator < 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
我正在使用Rails 3.0版
答案 0 :(得分:0)
我遇到了同样的问题。
为了解决这个问题,我在“config / lib”中创建了一个新文件夹“validators”。
然后我将其添加到config / application.rb:
config.autoload_paths += %W(#{config.root}/lib/validators/)
答案 1 :(得分:0)
修改用户模型,如下所示:
class User < ActiveRecord::Base
require "email_format_validator"
validates :email, :presence => true, :uniqueness => true, :email_format => true
end