Adding User Management with Devise - Ruby On Rails Friend List App #6

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys john elder here from codingame.com and in this video i'm going to show you how to add users to our friends list app with ruby on rails all right guys like i said in this video we're going to look at adding users to our friends list app but before we get started if you like this video you want to see more like it be sure to smash the like button below subscribe to the channel give me a thumbs up for the youtube algorithm and check out cody.com we have dozens of courses with hundreds of videos that teach you to code use coupon code youtube1 to get 30 off membership that's all my courses videos and books for one time fee is just 49 which is insanely cheap okay we are moving right along with our friends list app in this video i want to show you how to add users so we've got this website and we can add friends ourself but anybody could come along and add a friend to our list and that's no good we want to be able to log into this website ourselves have our own list in our own account that we only control and maybe you want other people to be able to sign up for an account log in add their own list you know and you want as a user you want them to be able to update their profile sign in sign out you know generate a password oh you forgot your password uh sign up all the things so if you look at this we can log in real quick here and you know we get a little message we can edit our profile and now we're going to bootstrapify these things and make them look nicer this is just the default stuff that comes out of the box and we'll get into all that later in this video we're just going to set all this up so you see up here sign out edit profile if we sign out you send out successfully and all that good stuff so we can sign up if we you know so all kinds of cool stuff forget your password so that's what we're going to look at in this video and with ruby on rails this is insanely easy we're gonna use a gem for this we haven't really talked about gems yet but gems are little third-party things that you can add to your app that just work right so they're super easy to use sort of like with django and python where you pip install something and then you can use it it's sort of like that but a little bit different so we're going to get into all of that in this video so the thing we're going to use is something called devise and devise is a gem that handles all of this user management stuff so let's head over to rubygems.org and this is the website that has all the gems that you could ever want it's sort of like the central repository website for gems and you can see there are let's see 59 billion downloads of different gems to date so this is crazy so let's come up here and let's just type in devise d-e-v-i-s-e and you can see there's a bunch of things we want the one that's been downloaded 82 million times so you can see this is a very popular gem a lot of times when you're using gems this is a good indicator of if it's going to be a good gem or not because you never really know right so if you find something that you're looking for and it has like six downloads you're going to want to be a little wary of it if it has 82 million downloads it's probably good so we can click on this device and here is the page where we can find the actual gem so here's to install it here's to add it to our gem file we want to add it to our jump file you're almost always going to want to add these to your gem files so and click on this little thing to copy to our clipboard so we can head back over to our code code and just come down here to our gem file and this is where all of our gems are located and we could just paste this in uh right click paste or control v to paste and now this is added so let's add the so let's save this file ctrl s to save it or come up here and file save now usually to add a gem to your app it's usually a couple step process you put it in your gem file and then you go back to your terminal and you type in bundle install and usually that does it it installs all the gems that are currently in your gem file but with device device is a little bit more complicated this is a lots of moving parts in this gem and it does a lot of stuff so there's several more steps so we're going to have to go through those steps but to start off we can just head over to the terminal and control c to break out of your terminal if your server's running and the first step is always just bundle install so that's bu b-u-n-d-l-e install so this will go to rubygems.org grab all the gems that it sees in your gem file and install them and if we come through here we could probably find there it is devise has now been added right so all right that's good now what do we have to do after that well we can head over the device website here or to the rubygems website and down here usually you can either go to the homepage or the documentation link and sometimes these go to the same place i'm going to click on documentation or we can go back and click on home page it goes back to the same thing this github page and this is the device github page and it has instructions on how to install it and how to use it so we could just keep scrolling down here to the getting started section first add your gem file we did that bundle install we did that now we need to run this command rails generate device install so i'm going to copy this head back over to my terminal here and right click and let's paste this in and this will give us a bunch of instructions after this that we then have to follow and they're not really that bad we've already done most of them but we're going to want to read through here so here's a list of things now we have to do the first one is the most important thing ensure you have defined default url options in your environments file for mailers so if somebody forgot their password and you want to automatically send them a password link or whatever we need to be able to do that so we need to config so we need to configure this file it's in config environments development.rb and what we need to do is add this line to it so i'm going to right click and copy this and let's go to our config environments development.rb file so let's head over to our code and that was config right there environments development rb so here's that file now we just come down here to the bottom where it says the very last end and let's push this down a little bit tab over and then paste in this line so what we're doing here is setting up an action mailer thing and our default is localhost so we need to set our host for that on our development environment so development rv rb is the file that works with development stuff we're developing right now we have not pushed this to production this is a development environment so we also need to do the same thing in our production.rb for when we push this live on the internet we haven't done that yet but we will eventually so i'm going to come down here and do the same thing again put some space tap this over but instead of local host and port 3000 we're going to want to put our actual host so if we push this for instance up to heroku for web hosting we'll put our heroku url there we don't have that yet though so instead i'm just going to comment this out and we'll leave this for later but i'm going to save this anyway and make sure we save this file as well so we can close these two files and head back over to our terminal and see what else is next you'll notice it also says do the same thing for production i just kind of skipped over that and just did it without reading it let's see number two we have to define a root url in our routes file so this we've already done that so we don't have to do that number three is ensure we have flash messages on our application.html page notice this we did that in the last video so we're good to go there too and then finally we need to set up views so when device starts working it's going to need a bunch of different web pages it's going to need a page to allow somebody to sign up it's going to need a page to allow somebody to sign in to sign out to edit their profile to you know send a password reminder all these different pages we need to create those pages and it will do it for us with this command so rails g short for generate right we can copy this device view so we can come down here and let's paste this in rails g uh devise views and boom boom boom it does its thing and you can see it's created all of this stuff and we're going to get into all of this stuff later if you want to kind of look through here and just sort of familiarize yourself with these for now like i said we're going to get into this later just know that all of these views have now been created in our app views devise folder remember in the last video we created our friends model stuff well two videos ago it generated all that stuff in app views friends now we're in app views device right so all of our views are usually like that so you can see through here there's a thing for passwords registration sessions mailer stuff all kinds of cool stuff and like i said we'll get into all that later so okay we're almost done let's head back over to the device website and we've done this and we did this now we need to create a model and the model is the database stuff right so device needs to keep track of all the users that sign up and sign in and sign out and edit their profile and it does that with a database so we need to set up that database by creating another table sort of like we did earlier when we generated our friends table where now we're going to create a user table that will handle all the user stuff so we can copy this line here and let's paste this in but instead of model we want to name this user because these are users we're going to name it user so all right notice singular and it usually takes a second okay so notice it's created a migration just like earlier with our friends migration there's some stuff here you can look at if you want and remember using databases is always a two-step process you create a migration then you push the migration we did that with our friends model so we've created a migration now we need to push that migration so let's go rails db colon migrate and it will go through here and push that migration and done and if we go back over to the website here you can see the next step of course is railsdb migrate so it's telling us to do that so you can look through here and read this this is useful to read and we'll refer back to this later on when we actually start using stuff but like right here this little helper we can run an if statement to say if a user is signed in then do certain things and we're going to do that in just a minute when we fiddle with our nav bar so think of our nav bar if a user signed in we want to show them a link to sign out if they're not signed in yet we don't need to show them a link to sign out because they can't sign out right so we're going to use this little helper for that you can see that who the current user is with this one do some user session stuff and all kinds of stuff and you can read through all of this stuff but for now we are good to go so we can head back over to our terminal and before we do that let me run the command rails routes so we created all these views now we need routes to those views and while this is doing its thing if we head back over to our code up there it's done if we head back over to our code real quick and look in config routes you see we've created new routes for device users right just like the resources for our friends it's all packed into this one line of code so that's cool but we need to know what those routes are so that's why i did this scroll up here rails routes command you can see here are all the new routes that is just created for us so we have this new user session that's to sign in let's see destroy user session that's to sign out new user password that's to get a password uh edit user password that's to edit your password what else do we have cancel if you want to close your your registration there's a route for that new user registration you want to sign up right there edit your profile you that's that one right there and some other things so let's start off right away by sort of tinkering with our nav bar so let's close this let's head over to app views you can see here's our device folder let's go to home and our header this is where our nav bar is and let's come up here and let's create some more links so we're going to need i don't know three or four of them so i'm just going to paste one two three four so the first one i lost track where we at so one two three four so this one will be sign up right this one will be sign in this one will be sign out and this one will be what do we think edit profile and that's good enough for now just to get us started here there's other things we can add but for now that looks good so sign up what's the route for that well let's head back over to our terminal and look so sign in sign out sign up so we want new user registration so i can copy this and remember it's going to be dash path so i can paste this in new user registration underscore path right so that's the sign up to sign in that's going to be let's see sign in right here so that's going to be new user session so i can copy that head back over here and new user session path remember they're all underscore path at the end so that's cool sign out we'll do that one in a second uh edit profile let's grab that one so i think that's down here edit user registration yeah so let's copy this head back over here and this is going to be edit user registration path now finally this one sign out this one's a little bit different and i'll show you here if we head back over here let's see sign in sign it sign out you'll notice the verb for all of these are get or post right this one is delete because what we're doing is when we sign in to devise we're creating a user session right so here when we sign in we're creating a new user session to sign out we destroy that session right so we delete that session so here is going to be destroy user session but then we need to modify it just a little bit and i'll show you how so let's head over here so sign out it's still going to be underscore path so destroy user session path but then here we need to put a comma and then type method delete and then a comma so that's how we do that for sign out so okay let's go ahead and save this and let's head back over to our terminal and come down to the bottom again and let's run our server rails s okay so it's up and running we can head back over to our website and now we've got these things sign up sign in sign out edit profile so let's sign up okay that works so let's go john e4196 gmail.com my password is whatever now i can sign up my passwords didn't mask and so you notice this is very cool it handles uh things like if you didn't type in the right password correctly right which i didn't just then so let me try again and like i said we're going to make these this all look nicer we'll bootstrapify it later on for now we just want the functionality to make sure it all works so let's click sign up this is welcome you have signed up successfully now up here this doesn't change yet but let's click edit profile and we can see email password we can cancel my account right all kinds of cool stuff if we want to sign out signed out successfully if we click edit profile now it's saying hey log in so now if we click sign out well it says sign out again so okay now we want to change this up here to only show certain links if we're signed in or signed out how do we do that well the device documentation tells us we can use this helper user signed in question mark so let's copy this head back over to our code and let's go let's find right here so sign up right off the bat let's create a ruby embedded ruby tag and notice we're not going to put an equal to sign like we usually do because this is just an if statement so if user signed in if they're signed in we want to show them what well we want to show them the link to sign out and edit their profile we don't need to show them the link to see to sign up or to sign in right so if the user signed in let's only show them these two links so i'm just going to copy these and delete them and come up here and tab over we don't have to tab over but it's easier to read if we do so if the user signed in we want to show this one and this one now we can come down here and go another tag else do this and this and we tab this over so okay and then down here at the end of this we need a final tag to end so this is our if statement end so we're saying if these are signed in show them this else show them this oops and then end so okay let's go ahead and save this and reload and see if that worked okay so we haven't signed in here so we have a link to sign up and sign in we can sign up here or we could sign in here let's sign in and forgot password let me open this in a new page hey that works that's cool so let's log in we signed in successfully it puts us straight to the edit profile page that's weird we can change that if we want and you'll notice up here let me get rid of this we have options to sign out or edit profile or add friends so right now this works so let's let's move these around too so let's say add friend we only want to add a friend if we're signed in otherwise we don't want people to be able to add friends so let's change that real quick so let's find our friends link let's see add friends right here so we can copy this and put that in if user signed in let's just paste this in and let me tab this over just to make it look nice save this head back over here let's reload nothing seems to change but if we sign out boom that add friends disappears so that's very cool we can do the same thing with the friends too and we will eventually but uh very cool and we've been working on this like 20 minutes maybe less and we have a fully functioning website user administration system like that's just boggles of the mind like how fast it is to create a professional user management system with rails it's just this easy 20 minutes boom we're done like i'm not kidding back in the day 15 years ago 20 years ago this would have taken me a month to code all this stuff up at least you know if i even could have it it's you know so complicated one person doing it alone oh that's horrible but this we slapped in a gym we installed it we followed the directions we added a couple of things to our website and boom this works just like that how cool is that so our app is really coming along it's really becoming more sophisticated and man this whole project we spent less than an hour on right it's pretty amazing and that's why we like rails so that's all for this video if you liked it be sure to smash the like button below subscribe to the channel give me a thumbs up for the youtube algorithm and check out coding.com where you can use coupon code youtube1 to get 30 off memberships they pay just 49 taxes all my courses over 45 courses hundreds of videos and the pds of all my best-selling coding books join over a hundred thousand students learning to code just like you my name is john elder from kodamy.com and i'll see you in the next video
Info
Channel: Codemy.com
Views: 6,632
Rating: 4.9240508 out of 5
Keywords: ruby on rails devise gem, ruby on rails devise api, ruby on rails devise authentication, ruby on rails devise tutorial, rails devise api, rails devise authentication, rails devise admin, rails devise gem, rails devise views, rails devise setup, rails devise generate views, ruby on rails user authentication tutorial, ruby on rails user profile, ruby on rails users, rails user authentication, rails user authentication devise, rails user profile, rails user registration
Id: 9K5YvsrKBRk
Channel Id: undefined
Length: 20min 29sec (1229 seconds)
Published: Thu Oct 01 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.