开发者

New validator class introduced in lib directory is not recognised and throws error

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'

my user model fil开发者_运维知识库e as below:-

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

my lib class introduced as below:

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

I'm using Rails version 3.0


I had the same problem.

To solve it, I created a new folder "validators" in "config/lib".

Then I added this to config/application.rb:

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


Modify the User Model like below:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜