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

Categories

0 votes
516 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 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...