我正在尝试从Rails控制台设置用户的(管理员)密码:
bundle exec rails console
> Spree::User.first.email
=> "admin@mysite.com"
> Spree::User.first.encrypted_password
Spree::User Load (1.1ms) SELECT "spree_users".* FROM "spree_users" LIMIT 1
=> "4ec556............................................."
> Spree::User.first.password='spree123'
Spree::User Load (1.0ms) SELECT "spree_users".* FROM "spree_users" LIMIT 1
=> "spree123"
> Spree::User.first.password_confirmation='spree123'
Spree::User Load (1.0ms) SELECT "spree_users".* FROM "spree_users" LIMIT 1
=> "spree123"
> Spree::User.first.save!
Spree::User Load (1.0ms) SELECT "spree_users".* FROM "spree_users" LIMIT 1
(0.3ms) BEGIN
(1.3ms) SELECT COUNT(DISTINCT "spree_users"."id") FROM "spree_users" LEFT OUTER JOIN "spree_roles_users" ON "spree_roles_users"."user_id" = "spree_users"."id" LEFT OUTER JOIN "spree_roles" ON "spree_roles"."id" = "spree_roles_users"."role_id" WHERE "spree_roles"."name" = 'admin'
(0.3ms) COMMIT
=> true
> Spree::User.first.encrypted_password
Spree::User Load (1.0ms) SELECT "spree_users".* FROM "spree_users" LIMIT 1
=> "1bc15d.............................................."
到目前为止一切顺利。看起来用户的新密码已更改并提交到数据库。但是,当我尝试稍后使用Web客户端登录并使用新密码时,它将失败并显示无效的身份/密码消息。
我甚至尝试使用Spree::User.first.reset_password!('spree123', 'spree123')
更新密码,但是无法登录。
Rails 3.2.12 狂欢1.3.2
知道我做错了什么吗?如何正确设置新密码?
感谢。
答案 0 :(得分:13)
问题是,每次你正在做Spree::User.first
时,它都会从数据库重新加载记录。这意味着您要在记录的一个实例上设置值,重新加载它,然后保存尚未实际更改的重新加载的模型。一个简单的方法是创建一个包含记录和更新的本地实例变量:
user = Spree::User.first
user.password='spree123'
user.password_confirmation='spree123'
user.save!
答案 1 :(得分:0)
Spree::User.first.update_attributes(password: 'password')