我正在运行“rails console”,然后运行以下命令:
User.create(name:"John", email:"test@email.com", password:"foo", password_confirmation:"foo")
我明白了:
(0.1ms) begin transaction
User Exists (0.2ms) SELECT 1 FROM "users" WHERE LOWER("users"."email") = LOWER('test@email.com') LIMIT 1
(0.1ms) rollback transaction
=> #<User id: nil, name: "John", email: "test@email.com", created_at: nil, updated_at: nil, password_digest: "$2a$10$mY0/9RgjwOU46ZYcSC0TFOCMxrPiqWTEHWe1K27O/3Ya...">
当我使用SQLite数据库浏览器检查sqlite数据库的文件时,我什么都没看到。
这是我的用户模型:
class User < ActiveRecord::Base
#these attributes can be modified by the users
attr_accessible :name, :email, :password, :password_confirmation
#ruby's way of calling a method below...
has_secure_password
#validation testing
validates :name, presence: true, length: { maximum: 50 }
#regular expression (there is an official one)
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
#and add it..
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
#validate password
validates :password, length: {minimum: 6}
validates :password_confirmation, presence: true
end
为什么没有在我的数据库中输入数据?
我输入的任何内容都会出现此错误!
例如:
1.9.3p125 :005 > User.create(name:"Smith", email:"smith@email.com", password:"foo", password_confirmation:"foo")
(0.1ms) begin transaction
User Exists (0.1ms) SELECT 1 FROM "users" WHERE LOWER("users"."email") = LOWER('smith@email.com') LIMIT 1
(0.0ms) rollback transaction
=> #<User id: nil, name: "Smith", email: "smith@email.com", created_at: nil, updated_at: nil, password_digest: "$2a$10$6nzyRJ0IplI6B4bSoQEtUOIcrbFVl1ix3EAKPGJZjZQf...">
我从未使用该电子邮件进入史密斯用户,我仍然得到“用户存在”!
编辑:
我收到了错误。密码限制是5我输入了3个字母的密码 所以当我输入这个:
User.create(name:"Smith", email:"smith@email.com", password:"foobar", password_confirmation:"foobar")
(0.1ms) begin transaction
User Exists (0.2ms) SELECT 1 FROM "users" WHERE LOWER("users"."email") = LOWER('smith@email.com') LIMIT 1
Binary data inserted for `string` type on column `password_digest`
SQL (1.7ms) INSERT INTO "users" ("created_at", "email", "name", "password_digest", "updated_at") VALUES (?, ?, ?, ?, ?) [["created_at", Mon, 12 Mar 2012 00:16:42 UTC +00:00], ["email", "smith@email.com"], ["name", "Smith"], ["password_digest", "$2a$10$v/FqAuUPpbdIJ44jVHxbKOJt/uoBTJVkP4KIhzJHNcF8rWPFfKusi"], ["updated_at", Mon, 12 Mar 2012 00:16:42 UTC +00:00]]
(266.9ms) commit transaction
=> #<User id: 1, name: "Smith", email: "smith@email.com", created_at: "2012-03-12 00:16:42", updated_at: "2012-03-12 00:16:42", password_digest: "$2a$10$v/FqAuUPpbdIJ44jVHxbKOJt/uoBTJVkP4KIhzJHNcF8...">
我的工作,但我仍然得到这个奇怪的用户存在错误...任何想法?
答案 0 :(得分:8)
刚到这里使用谷歌搜索,发现问题所在。实际上,这是一个垃圾错误信息。即使有一个干净的数据库,它也会出现。问题是:密码只有3个字符长,这将导致
的问题 validates :password, length: {minimum: 6}
所以,如果你尝试使用更长的密码(和确认),它应该工作。 (PS:我使用的是MySQL服务器,而不是SQLite,但我很确定错误是一样的)
答案 1 :(得分:8)
user = User.new
设置user
user.save
查看user.errors
答案 2 :(得分:2)
实际上,问题不在于“用户存在”,而真正的原因是密码太短。 您可以通过在rails控制台上输入user.errors来显示错误。
请参阅以下错误示例。
irb(main):013:0> user = User.new(name:"caiqinghua", email:"caiqinghua@126.com", password:"admin",password_confirmation:"admin")
=> #<User id: nil, name: "caiqinghua", email: "caiqinghua@126.com", created_at: nil, updated_at: nil, password_digest: "$2a$10$1d5jtpdkpl9hJPr/8s/dku1Y34.Aft/cAP5h/wrTN2sL...">
irb(main):014:0> user.save
(0.2ms) begin transaction
User Exists (0.3ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('caiqinghua@126.com') LIMIT 1
(0.2ms) rollback transaction
=> false
irb(main):015:0> User.all
User Load (0.6ms) SELECT "users".* FROM "users"
=> #<ActiveRecord::Relation []>
irb(main):016:0> User.create(name:"caiqing", email:"caiqing@126.com", password:"admin",password_confirmation:"admin")
(0.3ms) begin transaction
User Exists (0.3ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('caiqing@126.com') LIMIT 1
(0.1ms) rollback transaction
=> #<User id: nil, name: "caiqing", email: "caiqing@126.com", created_at: nil, updated_at: nil, password_digest: "$2a$10$Ossfc7NsL6/MjYVEjT5rJe/y4AiqdNZI2tCkrN1h8rHx...">
**irb(main):017:0> user.errors**
=> #<ActiveModel::Errors:0xba7d34c0 @base=#<User id: nil, name: "caiqinghua", email: "caiqinghua@126.com", created_at: nil, updated_at: nil, password_digest: "$2a$10$1d5jtpdkpl9hJPr/8s/dku1Y34.Aft/cAP5h/wrTN2sL...">, @messages={:password=>["is too short (minimum is 6 characters)"]}>
irb(main):018:0>
如果我将密码从“admin”更改为“admin123”,则没有任何错误。
irb(main):018:0>
irb(main):019:0* user = User.new(name:"caiqinghua", email:"caiqinghua@126.com", password:"admin123",password_confirmation:"admin123")
=> #<User id: nil, name: "caiqinghua", email: "caiqinghua@126.com", created_at: nil, updated_at: nil, password_digest: "$2a$10$dHb.jezaiomN.ZE0pazkOOHDdac/K386h7zsORF93HtQ...">
irb(main):020:0> user.save
(33.7ms) begin transaction
User Exists (9.9ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('caiqinghua@126.com') LIMIT 1
Binary data inserted for `string` type on column `password_digest`
SQL (108.8ms) INSERT INTO "users" ("created_at", "email", "name", "password_digest", "updated_at") VALUES (?, ?, ?, ?, ?) [["created_at", Fri, 27 Sep 2013 15:25:27 UTC +00:00], ["email", "caiqinghua@126.com"], ["name", "caiqinghua"], ["password_digest", "$2a$10$dHb.jezaiomN.ZE0pazkOOHDdac/K386h7zsORF93HtQb7wtj73Ha"], ["updated_at", Fri, 27 Sep 2013 15:25:27 UTC +00:00]]
(112.7ms) commit transaction
=> true
irb(main):021:0> User.all
User Load (0.6ms) SELECT "users".* FROM "users"
=> #<ActiveRecord::Relation [#<User id: 2, name: "caiqinghua", email: "caiqinghua@126.com", created_at: "2013-09-27 15:25:27", updated_at: "2013-09-27 15:25:27", password_digest: "$2a$10$dHb.jezaiomN.ZE0pazkOOHDdac/K386h7zsORF93HtQ...">]>
irb(main):022:0>
答案 3 :(得分:1)
您正在尝试在数据库中创建存在唯一验证的重复行。请参阅您的错误消息(“用户存在”):
User Exists (0.2ms) SELECT 1 FROM "users" WHERE LOWER("users"."email") = LOWER('test@email.com') LIMIT 1
查看模型中的行:
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
验证用户的电子邮件地址是唯一的。因此,您必须在数据库中有一行“test@email.com”作为电子邮件地址。
答案 4 :(得分:1)
也许您正在寻找错误的数据库。这是rails应用程序。并查看数据库中的balbla.development模式,因为您的问题无法确定必须有一行具有相同的电子邮件值
答案 5 :(得分:1)
我不是百分百肯定,但我认为
用户存在(0.4ms)SELECT 1 FROM“users”WHERE LOWER(“users”。“email”)= LOWER('USER@example.com')LIMIT 1
仅检查唯一性,因为无论您的查询是通过还是失败,它都会运行。
如果您密切关注结果,您将意识到成功的结果将以蓝色显示“用户存在”,而当您实际尝试插入重复用户时失败的结果将以红色显示“用户存在” (在我的情况下是紫色的)。
赖安
答案 6 :(得分:1)
我也遇到了这个问题,因为我的密码不长于6.所以我把它改成了更长的一个,它起作用了。但我不知道为什么这个例外命名为“User Existed”,或者是 我有点不对劲 BYW,我没有打开SQLIte数据库浏览器。
答案 7 :(得分:1)
这是密码。
如果要检查错误,请执行以下操作:
user = User.new(name:"John", email:"test@email.com", password:"foo",
password_confirmation:"foo")
user.save
user.errors