Tôi đang chạy "đường ray giao diện điều khiển" và sau đó lệnh sau:Dữ liệu không vào cơ sở dữ liệu SQLite - Ruby on Rails
User.create(name:"John", email:"[email protected]", password:"foo", password_confirmation:"foo")
và tôi có được điều này:
(0.1ms) begin transaction
User Exists (0.2ms) SELECT 1 FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1
(0.1ms) rollback transaction
=> #<User id: nil, name: "John", email: "[email protected]", created_at: nil, updated_at: nil, password_digest: "$2a$10$mY0/9RgjwOU46ZYcSC0TFOCMxrPiqWTEHWe1K27O/3Ya...">
khi tôi kiểm tra các tập tin của cơ sở dữ liệu sqlite bằng cách sử dụng trình duyệt Cơ sở dữ liệu SQLite Tôi không thấy gì cả.
đây là mô hình người dùng của tôi:
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+\-.][email protected][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
tại sao dữ liệu không được nhập vào cơ sở dữ liệu của tôi?
Tôi gặp lỗi này với bất kỳ điều gì tôi nhập!
này ví dụ:
1.9.3p125 :005 > User.create(name:"Smith", email:"[email protected]", password:"foo", password_confirmation:"foo")
(0.1ms) begin transaction
User Exists (0.1ms) SELECT 1 FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1
(0.0ms) rollback transaction
=> #<User id: nil, name: "Smith", email: "[email protected]", created_at: nil, updated_at: nil, password_digest: "$2a$10$6nzyRJ0IplI6B4bSoQEtUOIcrbFVl1ix3EAKPGJZjZQf...">
tôi không bao giờ bước vào một người dùng Smith với email đó, và tôi vẫn nhận được rằng "người dùng tồn tại"!
CHỈNH SỬA:
Tôi gặp lỗi. Giới hạn mật khẩu là 5 tôi đã nhập một mật khẩu 3 chữ vì vậy khi tôi gõ này:
User.create(name:"Smith", email:"[email protected]", password:"foobar", password_confirmation:"foobar")
(0.1ms) begin transaction
User Exists (0.2ms) SELECT 1 FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') 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", "[email protected]"], ["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: "[email protected]", created_at: "2012-03-12 00:16:42", updated_at: "2012-03-12 00:16:42", password_digest: "$2a$10$v/FqAuUPpbdIJ44jVHxbKOJt/uoBTJVkP4KIhzJHNcF8...">
tôi làm việc, nhưng tôi vẫn nhận được tài lạ này Tồn tại lỗi ... Bất cứ ý tưởng?
Có vẻ như bạn đã có người dùng với email 'test @ email.com' để xác thực ': duy nhất' cho email không thành công. – pjumble
đã chỉnh sửa câu hỏi của tôi –
Bạn nhận được kết quả gì nếu bạn: 'user = User.new (tên:" Smith ", email:" [email protected] ", mật khẩu:" foo ", password_confirmation:" foo ")' và sau đó: 'đặt user.errors'? – pjumble