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

查看:98
本文介绍了在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!无法大量分配受保护的属性:
encrypted_pa​​ssword

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天全站免登陆