Iteration I1: Adding Users

Let’s start by creating a model and database table to hold our administrators’ usernames and passwords. Rather than store passwords in plain text, we’ll store a digest hash value of the password. By doing so, we ensure that even if our database is compromised, the hash won’t reveal the original password, so it can’t be used to log in as this user using the forms:

 depot>​​ ​​bin/rails​​ ​​generate​​ ​​scaffold​​ ​​User​​ ​​name:string​​ ​​password:digest

We declare the password as a digest type, which is another one of the nice extra touches that Rails provides. Now run the migration as usual:

 depot>​​ ​​bin/rails​​ ​​db:migrate

Next, we have to flesh out the user model:

Get Agile Web Development with Rails 7 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.