我创建了一个用户然后去了user.save
并得到了这个=>虚假消息......我看到它说“用户存在”,但是当我去users = User.all
时,只有一个用户我创建了一小段时间。那么为什么说这个用户存在?
irb(main):003:0> user = User.new(:screen_name => "jeff holmes", :email => "jeffholmes@gmail.com", :password => "123456")
/Users/coreyholmes/RubymineProjects/worklink/app/models/user.rb:22: warning: regular expression has ']' without escape: /\A[A-Z0-9._%-]+@(A-Z0-9-]+\.)+[A-Z]{2,4}\z/
=> #<User id: nil, screen_name: "jeff holmes", email: "jeffholmes@gmail.com", password: "123456">
irb(main):004:0> user.save
(0.2ms) BEGIN
User Exists (0.3ms) SELECT 1 AS one FROM `users` WHERE `users`.`screen_name` = BINARY 'jeff holmes' LIMIT 1
User Exists (0.2ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY 'jeffholmes@gmail.com' LIMIT 1
/Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activemodel-4.2.1/lib/active_model/validations/format.rb:8: warning: regular expression has ']' without escape: /\A[A-Z0-9._%-]+@(A-Z0-9-]+\.)+[A-Z]{2,4}\z/
(0.1ms) ROLLBACK
=> false
更新 这是验证码
class User < ActiveRecord::Base
# Max and min lengths for all fields
SCREEN_NAME_MIN_LENGTH = 4
SCREEN_NAME_MAX_LENGTH = 20
PASSWORD_MIN_LENGTH = 4
PASSWORD_MAX_LENGTH = 20
EMAIL_MAX_LENGTH = 50
SCREEN_NAME_RANGE = SCREEN_NAME_MIN_LENGTH...SCREEN_NAME_MAX_LENGTH
PASSWORD_RANGE = PASSWORD_MIN_LENGTH...PASSWORD_MAX_LENGTH
validates_uniqueness_of :screen_name, :email
validates_length_of :screen_name, :within => SCREEN_NAME_RANGE
validates_length_of :password, :within => PASSWORD_RANGE
validates_length_of :email, :maximum => EMAIL_MAX_LENGTH
validates_format_of :screen_name,
:with => /\A[A-Z0-9_]*\z/,
:message => 'must contain only letters, ' + 'numbers, and underscores'
validates_format_of :email,
:with => /\A[A-Z0-9._%-]+@(A-Z0-9-+\.)+[A-Z]{2,4}\z/,
:message => 'common... enter a real email address'
end
答案 0 :(得分:2)
我怀疑这是问题的一部分:
/\A[A-Z0-9._%-]+@(A-Z0-9-]+\.)+[A-Z]{2,4}\z/
# ↑
# Is this supposed to be "(["?
P.S。这也是验证电子邮件地址的错误正则表达式。首先,现在有大量TLD超过四个字符。