AWS + Rails: How to Add Devise Users and Admins to a Rails App

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
do you want to add user and admin accounts to your rails application if so stick around to find out how I'm Thomas with branches' digital full stack developer obsessed with learning if you're interested in learning full stack development please subscribe below to receive new content in this a TBS Rails series tutorial we're gonna walk through how to install and configure the device gem into an existing rails application we're going to use the rails application from a previous tutorial which I'll link right here in the card and down here in the description so you can follow along we're gonna walk through how to create users and admin accounts as well as a quick profile page while a profile page is not strictly necessary here it just seems like a very relevant feature typically when adding users and admins the next thing you need to do is add profile pages so I figured it just made sense to combine these into one tutorial to save you time before we get started I want to mention that we're also gonna be using a TBS work male for our emails we set this up in the previous tutorial and connected this to our rails application so that we can programmatically send out email so if you haven't seen that video yet you might want to watch that first I'll link that up here in the card and then down here in the description we're gonna use this to combine with the devise gems pre-existing functionality of sending out emails for things like forgot password type functionality or really any other type of email email notification you'd like to send to your users with that being said let's get into the rails device users and accounts tutorial to get started we have our a two Bs rails app running locally so we're switch over to sublime text and add in the gems that we're gonna need we're to use the demise gem of course and then we're also gonna use the devise bootstraps gem this gem just styles our login and signup forms using Bichette for just save us some time and get things kind of cleaned up for us we'll save this file flip back over to another tab in our terminal and bundle install once that's complete we will run the device installer with rails generate or G for short devise : install as you can see this creates a lot of files initializer and then it have some steps that you need to complete here manually the first of these steps tells you that you need to ensure you have a default URL options this is something we accomplished in the previous tutorial when we attached SCS email to our rails application so if you're following along you can just skip step 1 step 2 once you ensure that you have a root URL we set in the previous tutorial of pages home step 3 wants you to ensure that you have flash businesses displayed in your application you can copy and paste these into your layouts application that HTML body or B as the comment is suggesting but instead we're gonna pull us out into a partial and we're going to display bootstrap styled flash notices so let's do that right now in an app use shared when you create a new file that we will call alert and this file will just paste out our alert template code and walk through it really quickly here we to loop through all flash notices this is a partial I use quite frequently so at the top you've just got a couple of variables to use as options next we're looping through all of our flash notices and we're gonna put them in a bootstrap style dismissible alert within the code itself we're gonna paste out a title and the content of the message this partial requires a new method in our application helper alert class by type there are different types of flash messages like info or error we're gonna catch these and then place the appropriate bootstrap style class that corresponds with that message type so that we can get clean styling so we'll go over to application helpers and we'll paste in our method this simple method takes in the different flash notice bio dirt tight and then translates that to a corresponding bootstrap class so an error would come back as an alert danger warning would come back as alert warning so that we can style them appropriately in Boots chef this is just a convenient way to quickly get clean boots chat flash notifications in our application now that we've created our alerts partially you want to make sure to add this into the abuse so we really want to layouts application on HTML BRB and add this just above our header where the render shared Alert this way all of our flash notification will pop out at the very top of our screen and because we've used our partial and application helper they'll be styled for boots Jack so with that complete let's go back to the terminal and see the last step in step four devices telling you that you can run the device use command to copy the views over to application for easy customization we're gonna skip this command as well as we're gonna be using device bootstrapped that has its own commit the next thing we're going to do is create our device model rails generate or G for short devise and our app is going to contain users so we will call it user this can be anything you'd like in our case though we're gonna have users so you'll hit enter this will create some files as well as a database migration we're going to check out in a second but first we will run the device bootstrap gem to generate our views now that we've created a device model and generated our device views Before we jump into an application we're gonna run one more migration to add a few columns to our user model so here I'm gonna run rails G migration add admins to users we're gonna add an admin column of type boolean that we're gonna set to false then we're gonna add a few more columns for our future profile page first we add a bio of tight text and then two more columns first name and last name which will each be tough type string since string is the default data type we don't need to worry about passing that data tight here so hit enter this will generate our magician for us now let's jump back into the application well go into the database migrate here you can see the migration that divides generated for us when we generated our user model next you can see the migration that we created ourselves to add a few more columns here we want to ensure that our admin boolean defaults to false next you can see we're adding our bio first-name and lastname with all that out of the way now we can run a rails DB migrate to add all of these new columns including our device users before we check out our application of the browser we have to remember that we installed some new gem so we'll need to break out of the server and restart it if we reload our application you can see that everything is working but nothing's really changed we still need to add in links to sign up and sign in to our new user accounts let's go back into the views let's click into our header partial where we currently have our links we're gonna be adding a few more links so instead of just outputting this here let's extract this into a partial then we'll create the new partial underscore naitch TM LDR B then it will paste in the content here you can see the home and videos links that we used previously as well as a few more scoped links that device makes available to us so first we did this convenient method to check if a user is signed in if they're not signed in then we will allow them to either log in or sign up using the new user session path or the new user registration path finally once signed up or signed in the user would have the opportunity to log out using the destroy user session path with the method of delete if we flip back over the browser and refresh the page you can see now that we have the new links from our partial so here let's sign up a new account first we'll enter in our email and then a password and click sign up here you can see our bootstrap styled flash message you can also see that the navigation has since now we are signed in we have the opportunity to log out here if we could log out then we see the navigation changes again so let's log back in if we flip back over to the terminal and enter the rails console we can check out our new user here you can see our new user that is not an admin for now let's just change this via the console user done first update now if people come to you you can see we've successfully updated them to an admin while this works in this one-off case for updating my specific user to an admin in practice would be very difficult if you had say 20 or 30 users that you wanted to load into your application too quickly and assign a few of them to admin accounts so if you stick around to the end I'll show you a seed file that I would typically use to execute this task quickly but now let's go back and create our profile page to create our profile page the first thing we want to do is generate a controller rails G controller this controller has to be made users since we have a user device model we have a couple of actions on this controller index to list all users show to look at an individual user and edit so that a current user can edit their own profile page go ahead enter it's very important that if you chose to generate a different devise model say for example member then this would be the members controller but in our case we generated a user device model so we're going to generate a user's controller next let's jump at the application and clean up the routes file you can see that each individual route was added instead let's clean this up a bit with a resource route with our index show edit and update routes the edit and update go hand-in-hand when updating our profile page though the update won't actually have a view here and then let's put this bill our device for users now that we have routes for our users and profile pages let's add a links in the shared partial so we'll go into our ad views shared nav that HTML later your B by default will allow everybody to see the users index page go ahead link to users path is going to be the users path put the pipe character just separate we have not yet style this page but we'll circle back to that in a minute first let's add our profile link here if the user is logged in then they can view their profile so if we refresh that link populates in the navigation if we click on it it works but of course we haven't styled this page do that we're gonna need to update our users controller app controllers users you can see the code that was generated for us I'll just paste the code in and then we can walk through it so the first thing you see here is the before action authenticate user this is a device's way of ensuring that the user is logged in then we're passing only on the show action so by default you'll be able to see our homepage and you'll be able to see our users index page but if you want to look at users profiles you're gonna have to be logged in to do so next in our in X action you can see we're just grabbing all of the users in our database and putting them into the at users variable this is the actions can be called for viewing any user as well as our own profile page so just gonna grab the user by params and set that as the instance variable user edit and update are a little bit different we don't want users to be able to edit anyone other than themselves so in this case we're setting the instance variable for user to the device current user variable same thing here when we're attempting to update params using the strong parameters user params this is just a security feature each user can only ever update parameters their own user account first let's take care of our show page so go ahead and paste in the code and walk through it here we're gonna output the users full name which is the method we had it yet created but will create that shortly next we're gonna output whether or not the user is an admin followed by their bio finally we're going to insert a link for the user to edit the profile if and only if the current user is on their own user page now let's go into the user model and create that full name method full name is going to equal the users first name and a space and then their last name next let's create the edit page and form here on the Edit Page we're just gonna change the headline to and a profile then within the content we will render a form partial that we're about to create so now let's create that form partial for underscore form dot html' BRB this is a typical rails form but we'll pass in the user model here will output errors if any when submitting the form next we're not allowed the user to edit their first name last name and bio finally they can either submit the form or cancel the form and go back to the users path if we flip back over the browser and click on the profile page you can see that this user is considered an admin but they don't have a bio yet let's click the edit profile button we forgot to pass in the instance variable to the partial so let's go back over to our edit form and pass in user as the instance variable of the user then we'll refresh the page I just want to interrupt for one second and see if you're finding value please subscribe below hit the like button turn on the bail notification for for future notifications of content like this and if you are we have a limited time offer coworker here bear will perform one trip per subscribe down yes oh my gosh we're going viral bear now you can see we're editing our personal profile and then click update user first and last name our output along with the fact that we are an admin and then our bio of hello world next let's create our users index page here which can loop through all of our users the instance variable that we send the controller and then output an unordered list with a list item containing the users full name if we flip over to the browser click on users you can see all of our users listed out I want to walk through this partial to show a few ways that we could optimize it here we're iterating through all of our users as a single user instance and then outputting some code well this is very simple it would be nice to extract this into a partial so let's call this underscore user dot HTML BRB and then we'll paste it code then we will render the partial inside of a loop pass in the user variable now that we're iterating over users and outputting a singular user partial this is a prime candidate for collection or rendering just as in our rails app tutorial with videos were going to collection render out our users to do so just delete this line pleat this line here and instead equals render users and rails knows to loop through each user and render the user partial as you can see that still works but it ends up being a lot cleaner if we need to edit an individual user instance we can open our new user partial and make changes there this last step is optional but it's a way to quickly create several users at once through a seed file so for example you want to load dozens of users into your app by default you could create entries in this file and then run the task to populate that data well this isn't required it may be convenient if you have a lot of users that you want to add in bulk so I'll just paste this in and walk you quickly through the code first we have an array of hash objects the hash objects each contain an individual user we'd like to add in this case I'm adding a second user for myself that I'm saying is Thomas at example.com my first name my last name and then I'm saying that I am an admin true the next I'm adding my coworker bear the dog first name bear lastname Bush and he's not going to be an admin next we're gonna loop through all of our users in our array above we'll take each instance and search for a user with that email in our database we'll grab either the first one we find or we'll initialize a new record then we're going to update a record with all of the other coalos first-name lastname we'll set the admin value and then we're just gonna set a secure a random UUID as word and then we'll save the user as devised automatically encrypts your passwords you'd never be able to recover this password but since we already have forgot password functionality in device by default and we already have our email hooked up to our rails application with SCS this is not a problem in your documentation after you launched your app you'd simply instruct your users to navigate to your new application and choose the forgot password link here you can create as many rows as you need then to run this file flip back over to command line and run rails DB seed this is typically a one-off command meaning you're just gonna run it one time when you deploy your application but it's nice if you needed to deploy your app in the into production and then quickly bulk add dozens of users at once if we refresh you can see I've got my two users probably not the best to choose to name them identically but here is my first original user that is an admin with bio here is the second user that is an admin but has no bio because it was generated with a C file lastly you can see my coworker bear who is not an admin you can add additional columns to this users array of hashes if you'd like here is an example of adding the BIOS remember to add that column into your loop so here we're going to add bio that we'll take from the user instance bio key then if we see the database again and refresh the page you can see that Bears bio has come through so I hope you found that useful please remember to Like and subscribe as this really helps out the channel and feel free to leave questions or requests in the comment section below and I'll catch you in the next AWS Rails tutorial
Info
Channel: BrainTrust Digital
Views: 2,660
Rating: undefined out of 5
Keywords: aws + rails how to devise users and admins, add devise users, add users in a rails app, rails add users, devise users and admins, rails devise admin, rails devise users, how to add devise, devise rails, devise gem, devise tutorial, configure devise gem, rails devise, aws rails, aws+rails, devise install, how to add users, aws, ruby on rails, ruby on rails tutorial, ruby on rails 2020, amazon web services, ruby on rails aws, devise tutorial for beginners, Ruby on rails 6
Id: 8UIG9Ggu9Q4
Channel Id: undefined
Length: 20min 44sec (1244 seconds)
Published: Sun May 03 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.