2

我正在尝试从 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

知道我在做什么错吗?如何正确设置新密码?

谢谢。

4

2 回答 2

13

问题是每次你这样做时都会Spree::User.first从数据库中重新加载记录。这意味着您正在为记录的一个实例设置值,重新加载它,然后保存实际上没有更改的重新加载的模型。解决此问题的一种简单方法是创建一个包含记录的本地实例变量并更新它:

user = Spree::User.first
user.password='spree123'
user.password_confirmation='spree123'
user.save!
于 2013-02-20T01:30:48.333 回答
0
Spree::User.first.update_attributes(password: 'password')
于 2017-08-27T16:22:13.450 回答