Locked lesson.
About this lesson
Allow people to sign up to your site, log in, and log out. Part 2.
Exercise files
Download this lesson’s related exercise files.
Devise Installation Part 2.docx58.8 KB Devise Installation Part 2 - Solution.docx
58.6 KB
Quick reference
Devise Installation Part 2
In this video we'll continue to install the Devise gem and set up our User model.
When to use
Devise is used to handle user management for our website.
Instructions
To continue installing Devise, run this command from your terminal to create a database migration:
rails generate devise user
Next: add this line to your newly created migration:
t.string :name, null: false, default: ""
Next: push your newly created migration to the local database:
rake db:migrate
Next: push your migration to the production database at Heroku:
heroku run rake db:migrate
Hints & tips
- Remember, creating a database is a 2-step process (1. create a migration 2. push the migration into the database)
- rails generate devise user To generate a user model migration
- rake db:migrate to push our migration into the local database
- heroku run rake db:migrate to push our migration into the Heroku production database
- Don't forget to add a name field to the migration file before you push it
- 00:04 Okay, in the last video, we started to install Devise, and
- 00:08 we got all of these directories created for us.
- 00:10 Now, before we dive into them,
- 00:12 we need to do a couple more things to finish installing Devise.
- 00:15 So go back to the Devise documentation, and we got this far in the last video.
- 00:20 Now, we need to come down here, we need to create an actual model for our app.
- 00:24 And a model, as you remember in the MVC diagram earlier, model is database stuff.
- 00:29 So what we need to do here is create a database.
- 00:32 So pretty easy, copy that, come back, paste.
- 00:34 Now, we wanna change this word MODEL, this is just placeholder.
- 00:38 We wanna name this something else.
- 00:39 So what are we doing here in our app?
- 00:41 We're allowing people to become users of our website.
- 00:44 A user logs in, logs out, does stuff like that.
- 00:47 So let's call our model user.
- 00:49 So if we hit Enter, Devise is creating all this stuff for us.
- 00:53 And now check this out.
- 00:54 If we come over to our db directory, that's database, and
- 00:58 we click on this migrate, we now have this migration here.
- 01:01 And Devise just created this for us.
- 01:03 Any time you do database work in Ruby on Rails, it's a two-step process.
- 01:07 You create a migration, then you push the migration to the database.
- 01:10 And that might seem like a little bit complicated now, but
- 01:12 as we do it a couple times, it'll make a lot of sense.
- 01:14 And this makes it very easy.
- 01:16 And if you look through this migration,
- 01:18 you can see some of the stuff that we'll be creating.
- 01:20 Our users will be able to have an email, password to sign in, sign out, and
- 01:24 do all kinds of stuff.
- 01:26 Now, one change we wanna make before we do anything else, we need to add something.
- 01:31 And right here, I'm just gonna make a space and
- 01:34 type in t.string, space, and let's call this name.
- 01:38 And then give this a null: false, default.
- 01:46 And basically I just copied this, line but replaced email with name.
- 01:52 We want our users to be able to list their name, as well as their email address.
- 01:56 So save this, go ahead and close it.
- 01:58 So we've created a migration, now we need to push it into the database.
- 02:03 And we do that with this rake db migrate command.
- 02:07 So copy this, come back here, paste that in and hit Enter.
- 02:13 And it looks like it worked.
- 02:15 Now we can come to this db schema file, and
- 02:19 this shows us at a glace what our database currently has.
- 02:23 It's named users, it has email, name, password and all of this stuff.
- 02:26 So just like that, we now have a fully functioning database.
- 02:30 Actually, there is one more step.
- 02:32 We pushed the migration to our local database,
- 02:34 we also need to push this migration up to Heroku.
- 02:37 Because Heroku, if you remember, it uses the PG database,
- 02:40 not the SQLite3 database that we're using in our development environment.
- 02:45 So it's a similar command to the one we just did,
- 02:49 that rake db migrate, instead it's heroku run rake db:migrate.
- 02:56 So the last command was rake db migrate, now it's heroku run rake db:migrate.
- 03:02 And that's just the convention to use Heroku.
- 03:04 And you can see, now this is pushing our database up to Heroku,
- 03:08 it can take 30 seconds to a minute or so.
- 03:12 So we're pretty much done now, that's all of the installation instructions.
- 03:16 Now we can come back and take a look at all of these directories, and
- 03:20 folders, and files, and things and see what happened here.
- 03:23 So if we come down to our terminal and type rake routes, like we've done earlier.
- 03:28 In the past, we had one or two routes, now, look at all these routes we now have.
- 03:32 And take a look at these, user session, a new password,
- 03:36 user registration, new user registration.
- 03:39 So let's check this out, this is the path, this is the URI pattern.
- 03:44 So users/sign_up, let's copy that and
- 03:49 come to our app and paste it in.
- 03:53 You know what, we need to restart our server.
- 03:56 Whenever you add a new gem, usually you need to restart your server.
- 04:00 Give it a second to do its thing.
- 04:02 Come back here and hit reload, and boom ,now we have this sign up form.
- 04:06 We have, click this link, we have a log in form, if you forgot your password,
- 04:10 you have a forget password form.
- 04:12 So this all works right now, right out of the box.
- 04:15 It's fully functioning, and it connects to our database.
- 04:18 So if a user signs up, their name gets added to our database, and
- 04:21 it's in there forever.
- 04:22 So that's really cool.
- 04:23 In the next video, we'll start to customize these 'cause they look kinda funky
- 04:27 right now, we can make them look a little bit better.
- 04:29 But just like that, we've got Devise up and running,
- 04:31 and we have a fully functioning user authentication system that
- 04:34 would have taken, I kid you not, a team of developers months to create on their own.
- 04:38 Took us less than ten minutes to install, so really awesome, really cool.
- 04:42 And that's all for this video.
Lesson notes are only available for subscribers.