Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...