gpt4 book ai didi

ruby-on-rails - 数据未进入 sqlite 数据库 - Ruby on Rails

转载 作者:行者123 更新时间:2023-12-04 07:33:50 25 4
gpt4 key购买 nike

我正在运行“rails 控制台”,然后运行以下命令:

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

我从未使用该电子邮件输入 Smith 用户,但我仍然收到“用户存在”!

编辑:

我得到了错误。密码限制为 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...">

我工作,但我仍然收到这个奇怪的用户存在错误......有什么想法吗?

最佳答案

刚刚使用 Google 搜索来到这里,发现了问题所在。事实上,这是一个垃圾错误信息。即使使用干净的数据库,它也会出现。问题是:密码只有 3 个字符长,这会导致问题

    validates :password, length: {minimum: 6}

因此,如果您尝试使用更长的密码(和确认),它应该可以工作。
(PS:我使用的是 MySQL 服务器,而不是 SQLite,但我很确定错误是一样的)

关于ruby-on-rails - 数据未进入 sqlite 数据库 - Ruby on Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9660198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com