我们最近开始使用Clearance gem,但遇到了一些问题,我的自定义验证不再有效
验证
validates :first_name, presence: true, if: :registered_user?,
format: { with: CustomValidators::Names.name_validator },
length: { maximum: 30 }
validates :last_name, :presence => true, :if => :registered_user?,
:format => { :with => CustomValidators::Names.name_validator },
:length => { :maximum => 35 }
validates :email, :presence => true,
:uniqueness => true,## This should be done at the DB this is too expensive in rails
:format => { :with => CustomValidators::Emails.email_validator },
:length => { :maximum => 255 }
validates :mobile, :presence => true,
:uniqueness => true,## This should be done at the DB this is too expensive in rails
:format => { :with => CustomValidators::Numbers.phone_number_validator },
:length => { :maximum => 10 }
custom_validators.rb
module CustomValidators
class Emails
# please refer to : http://stackoverflow.com/questions/703060/valid-email-address-regular-expression
def self.email_validator
/\A(|(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6})\z/i
end
end
class Numbers
# validates australian numbers
def self.phone_number_validator
/\A(04[0-9]{8})\z/
end
# validates australian postcodes
def self.postcode_validator
/^(0[289][0-9]{2})|([1345689][0-9]{3})|(2[0-8][0-9]{2})|(290[0-9])|(291[0-4])|(7[0-4][0-9]{2})|(7[8-9][0-9]{2})$/
end
end
class Names
def self.name_validator
#/([a-zA-Z-’'` ].+)/ \A and \z
#/^([a-z])+([\\']|[']|[\.]|[\s]|[-]|)+([a-z]|[\.])+$/i
#/^([a-z]|[\\']|[']|[\.]|[\s]|[-]|)+([a-z]|[\.])+$/i
/\A([[:alpha:]]|[\\']|[']|[\.]|[\s]|[-]|)+([[:alpha:]]|[\.])+\z/i
end
end
清除初始
Clearance.configure do |config|
config.allow_sign_up = true
config.cookie_domain = '.club.town'
config.cookie_expiration = lambda { |cookies| 1.year.from_now.utc }
config.cookie_name = "remember_token"
config.cookie_path = "/"
config.routes = true
config.httponly = false
config.mailer_sender = 'Cup Town <info@cup.town>'
config.password_strategy = Clearance::PasswordStrategies::BCrypt
config.redirect_url = "/"
config.secure_cookie = false
config.sign_in_guards = []
config.user_model = User
Clearance::SessionsController.layout 'landing'
end
错误消息
电子
xiting
/Users/paulmcguane/RoR/barista/app/models/user.rb:77:in `<class:User>': uninitialized constant User::CustomValidators (NameError)
from /Users/paulmcguane/RoR/barista/app/models/user.rb:1:in `<top (required)>'
from /Users/paulmcguane/RoR/barista/config/initializers/clearance.rb:15:in `block in <top (required)>'
from /Users/paulmcguane/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/clearance-1.14.1/lib/clearance/configuration.rb:177:in `configure'
from /Users/paulmcguane/RoR/barista/config/initializers/clearance.rb:1:in `<top (required)>'