Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
519 views
in Technique[技术] by (71.8m points)

rails 3 customize validator error

I am following Ryan Bates' Railscast. I find when I tried to establish my customized field validator, my rails 3 is not working as expected.

I established a new email_format_validator.rb file in under lib/ and the codes are:

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 put this line in my user.rb (Model file):

validates :email, :presence => true, :uniqueness => true, :email_format=>true

The browser complained:

Unknown validator: 'email_format'

Why? How to solve this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You will need to restart your server. The lib directory isn't loaded by default, so you'll need to restart your Rails server in order to load this validator.

 

Edit:

Try putting them under lib/validators and restarting the server...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...