简体   繁体   English

Hartl Rails教程 - 种子用户密码

[英]Hartl Rails Tutorial - Seed User Passwords

I'm working my way through the excellent Rails tutorial by Michael Hartl, and I'm trying to wrap my head around an aspect of the seed users. 我正在通过迈克尔·哈特尔(Michael Hartl)的优秀Rails教程工作,我试图围绕种子用户的一个方面。 I just finished chapter 9, so I've got a working login/logout/edit/delete system with admin privileges. 我刚刚完成了第9章,所以我有一个具有管理员权限的工作登录/注销/编辑/删除系统。 As part of this chapter, we create some dummy users in the fixtures file: 作为本章的一部分,我们在fixtures文件中创建了一些虚拟用户:

michael:
  name: Michael Example
  email: michael@example.com
  password_digest: <%= User.digest('password') %>
  admin: true

My question is: how do I login as michael@example.com? 我的问题是:我如何以michael@example.com身份登录? It's not clear to me what the password is. 我不清楚密码是什么。 On a related note, what is the best practice for creating a universal "admin" user? 相关说明,创建通用“管理员”用户的最佳做法是什么? Should I use a fixture, or should I add one to the database through the Rails console before deploying the app? 我应该使用夹具,还是应该在部署应用程序之前通过Rails控制台将数据库添加到数据库?

Thanks for your help! 谢谢你的帮助!


Edit - here's the link to the BitBucket repository: https://bitbucket.org/jonathan_buck/sample_app/overview 编辑 - 这是BitBucket存储库的链接: https//bitbucket.org/jonathan_buck/sample_app/overview

You're generating password digest from string "password" 您正在从字符串“password”生成密码摘要

User.digest('password')

So user's password is "password" 所以用户的密码是“密码”

The password is not saved as-is in the database. 密码不会按原样保存在数据库中。 Instead a digest/secured version of the same is saved. 而是保存相同的摘要/安全版本。 The method #digest takes a parameter and outputs the digest version. #digest方法接受一个参数并输出摘要版本。

So, technically, the set password is "password". 因此,从技术上讲,设置密码是“密码”。 But in the database, you'll find it gibberish. 但在数据库中,你会发现它是乱码。

Makes sense? 说得通?

To address the main question what is database seed. 解决什么是数据库种子的主要问题。 db/seed.rb is a very nifty thing that helps you to seed some test users really fast. db / seed.rb是一个非常漂亮的东西,可以帮助您快速为一些测试用户提供种子。 As you can see it has the .rb extension so it is a normal ruby program nothing unusual. 你可以看到它有.rb扩展名,所以这是一个普通的ruby程序没什么不寻常的。 You have access here to all your Models and don't have to enter everything manually from console. 您可以访问所有模型,无需从控制台手动输入所有内容。 Well I guess what's the deal with password you already figured out form the previous answers. 好吧,我想你已经从之前的答案中找到的密码处理是什么。 Just take a look at the method digest, and on SitePoint is the perfect article about how user should be authenticated. 只需看看方法摘要,在SitePoint上是关于如何对用户进行身份验证的完美文章。 And for your final question take a look at this article , it will help you understand the concept 对于您的最后一个问题,请看一下这篇文章 ,它将帮助您理解这个概念

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM