在我的应用程序中,当我尝试使用
创建用户时 User.create(email:"jnan@gmail.com",encrypted_password: "foobar",name:"jnan")
显示
(0.1ms) begin transaction
User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = 'jnan@gmail.com' LIMIT 1
(0.1ms) rollback transaction
=> #<User id: nil, email: "jnan@gmail.com", encrypted_password: "foobar", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: nil, updated_at: nil, name: "jnan">
我的用户包含
=> User(id: integer, email: string, encrypted_password: string, reset_password_token: string, reset_password_sent_at: datetime, remember_created_at: datetime, sign_in_count: integer, current_sign_in_at: datetime, last_sign_in_at: datetime, current_sign_in_ip: string, last_sign_in_ip: string, created_at: datetime, updated_at: datetime, name: string)
答案 0 :(得分:4)
不应设置encrypted_password
,而应设置password
和password_confirmation
:
password = 'foobar'
User.create(email:'jnan@gmail.com',password: password, password_confirmation: password, name: 'jnan')
答案 1 :(得分:3)
Marek是对的,你应该在创建任何记录时设置密码和password_confirmation。你不应该使用encrypted_password。
当您传递密码和password_confirmation来设计它时,使用属性访问器检查两者是否相等。如果验证顺利,Devise会在内部创建一个md5 sting,用于保存在encrypted_password feild中的密码。所以没有人能直接看到密码。
2.1.0 :002 > User.create(email:"jnan@gmail.com",encrypted_password: "foobar")
(0.1ms) BEGIN
User Exists (124.5ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = 'jnan@gmail.com' LIMIT 1
(0.2ms) ROLLBACK
=> #<User id: nil, email: "jnan@gmail.com", encrypted_password: "foobar", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, confirmation_token: nil, confirmed_at: nil, confirmation_sent_at: nil, unconfirmed_email: nil, created_at: nil, updated_at: nil, first_name: nil, last_name: nil, profile_image: nil, gender: nil, date_of_birth: nil>
2.1.0 :003 > User.create(email:'jnan@gmail.com',password: 'password', password_confirmation: 'password')
(0.2ms) BEGIN
User Exists (0.6ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = 'jnan@gmail.com' LIMIT 1
User Load (24.7ms) SELECT "users".* FROM "users" WHERE "users"."confirmation_token" = '1442820d0b7521559ec07584408c6fdb6490444117383d76f6bf1dfc6dd90af6' ORDER BY "users"."id" ASC LIMIT 1
SQL (246.6ms) INSERT INTO "users" ("confirmation_sent_at", "confirmation_token", "created_at", "email", "encrypted_password", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["confirmation_sent_at", Tue, 08 Sep 2015 08:24:55 UTC +00:00], ["confirmation_token", "1442820d0b7521559ec07584408c6fdb6490444117383d76f6bf1dfc6dd90af6"], ["created_at", Tue, 08 Sep 2015 08:24:54 UTC +00:00], ["email", "jnan@gmail.com"], ["encrypted_password", "$2a$10$Uw9088ovsHC/tCeEN0aJGeI83ax7bjXD2RcQHjvlLg.SovJcyz1MS"], ["updated_at", Tue, 08 Sep 2015 08:24:54 UTC +00:00]]
答案 2 :(得分:0)
您需要将唯一的电子邮件发送到数据库。
旧的jhan@gmail.com
答案 3 :(得分:0)
这也有效(没有password_confirmation
)
user = User.create(name: "raj",email:"raj@gmail.com",password: "foobar")