Rails 3, Unknown validator: 'EmailValidator'

前端 未结 3 1168
我在风中等你
我在风中等你 2021-01-17 10:41

I try to add an email-validator in my rails app. I created the following file /lib/validators/email_validator.rb

class EmailValidator < Activ         


        
相关标签:
3条回答
  • 2021-01-17 11:24

    If you place your custom validators in app/validators they will be automatically loaded without needing to alter your config/application.rb file.

    Resource: Where should Rails 3 custom validators be stored? (second answer)

    0 讨论(0)
  • 2021-01-17 11:25

    Try the modified User model;

    class User < ActiveRecord::Base
    
      attr_accessible :email, :password,:name
    
      validates :email, :presence => true, :uniqueness => true
    
    end
    
    0 讨论(0)
  • 2021-01-17 11:31

    This error occures, because rails loads model file before your validation file

    Try to require your validation file manually at the start of your model file

    require_dependency 'validators/email_validator.rb'
    
    0 讨论(0)
提交回复
热议问题