2010-10-10 25 views
3
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' 

我的用户模型如下文件: - :新的验证器类不被识别并引发错误

:~/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 

我介绍如下

class User < ActiveRecord::Base 
    validates :email, :presence => true, :uniqueness => true, :email_format => true 
end 

我的lib类m使用Rails版本3.0

回答

0

我有同样的问题。

为了解决这个问题,我在“config/lib”中创建了一个新的文件夹“validators”。

然后我将此添加到配置/ application.rb中:

config.autoload_paths += %W(#{config.root}/lib/validators/) 
0

修改用户模型象下面这样:

class User < ActiveRecord::Base 
    require "email_format_validator" 
    validates :email, :presence => true, :uniqueness => true, :email_format => true 
end