无法使用 rake db:seed for Rails 3.0 创建 Devise 帐户
我正在尝试使用 rake db:seed 提前预加载所有设备帐户。所有其他模型的数据似乎都插入到数据库中,但由于某种原因,没有为使用 devise 的 Person 模型创建任何行。从网络界面注册工作正常,但我想避免手动创建帐户,这就是我使用 rake db:seed 的原因。我从通过网络界面创建的帐户复制了 crypto_password,password_salt。请让我知道如何解决这个问题?非常感谢..
people = Person.create(
:email => '[email protected]',
:encrypted_password => '$2a$10$SyacAOhJQtVeTcTPYm.ROuFbhGMylfj4fLrK3NHyeRwfEokKp2NVW',
:password_salt => '$2a$10$SyacAOhJQtVeTcTPYm.ROu',
:first_name => "nnn",
:last_name => "yyy"
)
in routes.rb i have.
devise_for :people
I'm trying to preload all devise accounts in advance using rake db:seed. Data for all other models seems to be inserted in the database but for some reason NO rows are created for Person model which uses devise. Registration from web interface works fine, but I want to avoid creating accounts manually, thats the reason i'm using rake db:seed. I copied encrypted_password,password_salt from an account created via web interface. Please let me know how to get around this? Many thanks..
people = Person.create(
:email => '[email protected]',
:encrypted_password => '$2a$10$SyacAOhJQtVeTcTPYm.ROuFbhGMylfj4fLrK3NHyeRwfEokKp2NVW',
:password_salt => '$2a$10$SyacAOhJQtVeTcTPYm.ROu',
:first_name => "nnn",
:last_name => "yyy"
)
in routes.rb i have.
devise_for :people
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我过去曾使用 Devise 完成过此操作。我没有尝试以这种方式设置加密密码和盐。我只是设置了密码和确认,如下所示(我手边没有我的项目):
尝试一下。
I've done this using Devise in the past. I didn't try setting the encrypted password and salt that way. I just set the password and confirmation something like this (I dont have my project handy):
Try that.
最有可能的是,“create”方法由于模型验证而悄然失败,因此返回 false。如果您使用“create!”,您就会播下错误。 method 相反(带感叹号) - 如果验证失败,此方法会引发异常。
在您的情况下,验证失败的可能原因是(Devise 默认情况下)最小密码长度为 6 个字符,而您根本没有提供密码。
Most likely the "create" method fails quietly due to model validation and therefore returns false. You would've seed the errors if you used "create!" method instead (with exclamation) - this method raises the exception if validation fails.
The likely reason for validation failure in your case is that (by default for Devise) minimum password length is 6 characters and you were not supplying password at all.
如果您不想在多次运行 rake db:seed 时出现重复项:
If you don't want duplicates while running rake db:seed multiple times: