在 Ruby on Rails 中使用 Devise 播种用户 [英] Seeding users with Devise in Ruby on Rails

查看:32
本文介绍了在 Ruby on Rails 中使用 Devise 播种用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的开发和测试环境中,我想用一堆用户为数据库做种子.我正在使用 Ruby on Rails v3.2.8 和最新的 Devise.所以我在我的 db/seeds.rb 文件中添加了这一行:

In my development and test environments, I want to seed the database with a bunch of users. I'm using Ruby on Rails v3.2.8 and the latest Devise. So I added this line in my db/seeds.rb file:

User.create(email: 'test@example.com', encrypted_password: '#$taawktljasktlw4aaglj')

但是,当我运行 rake db:setup 时,出现以下错误:

However, when I run rake db:setup, I get the following error:

耙子中止!不能批量分配受保护的属性:加密密码

rake aborted! Can't mass-assign protected attributes: encrypted_password

播种用户的正确方法是什么?

What is the proper way to seed users?

推荐答案

你必须这样做:

user = User.new
user.email = 'test@example.com'
user.encrypted_password = '#$taawktljasktlw4aaglj'
user.save!

阅读本指南以了解什么是批量赋值:http://guides.rubyonrails.org/security.html

Read this guide to understand what mass-assignment is: http://guides.rubyonrails.org/security.html

我想知道为什么一定要直接设置加密密码.你可以这样做:

I am wondering why do have to directly set the encrypted password. You could do this:

user.password = 'valid_password'
user.password_confirmation = 'valid_password'

这篇关于在 Ruby on Rails 中使用 Devise 播种用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆