Let's Build: With Ruby on Rails - Discussion Forum - Creating Admin Roles - Part 8

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] all right welcome back this next part is going to be discussing the admin type of user role associated with a typical app so I'm gonna set my main account which is the web crunch one here here to be an admin and currently we're getting an error because we don't have this role that exists and rails has no idea what this has all means so we'll go through the steps doing this we're gonna be using a couple of gems called let's see what they're called Kin Kin Kin is one and rolla phi as the other they work together to kind of integrate into devise in a decent manner and you can set up different roles based on your app so for a forum it might make sense to create an admin role and moderation role and maybe a editor role you can do those to your heart's content I don't really need that for this small of an app because this is one of us an exercise by we're building a full-fledged forum I would definitely go that route though and maybe hook into devise in vital it's to where I can invite users with that role but that's a topic for another time and that's probably an even longer video so I will go crazy there but I definitely recommend extending this if you're interested so we have those gems installed so the next thing I really want to do is kind of look to a tutorial I found on devise on the website let me see if I can find you your so-called rola fie community yeah so you can add devise to kinking can with rola fie and it kind of just walks you through the steps here I'll show you how to do that in a more concise way but I'm just wanted to show you that that resource is out there if you want to go do it yourself but this will kind of teach you through how that works so the first thing we want to do is create a couple generations that are built into those gems and we can do so by running rails generate can can let me verify that's 100% correct I'm gonna office three or one its can-can okay cool and then ability is what we're looking for so this is going to generate an ability model and in that well while we're at it let's go ahead and generate a role of Phi user believe that works I mean again check the article I think I have typing right yeah you have basically fall in tomorrow so let's do that it tells you what it did it creates an initializer file you can kind of customize if you want to do some stuff there but I'm gonna leave it as is so next we'll actually go into our database and do a rake DB migrate you can run rails DB migrate there too if you want and should create a join table which is users roles and it's just a way of communicating roles with users based on device so we define that by creating this generation or generator before which was right here so we define those two things so cool now in our model that was generated I'm gonna go to the app model and there'll be two new models one role which I believe we won't have to touch and then ability ability we do so in this one I'm going to basically create just a single admin user and that's about it you can define this to be whatever you want but really all we need to do is uncomment this and would be set but I'm gonna make this instead of just that admin I'm going to put a has role and the question mark and then : admin and you can delete these comments if you want so basically this is saying for each new user who has a role of admin they can manage all otherwise they can read all it's just kind of the way it works and then on a roll we should be set there the next thing we need to do is add resource of Phi to our models that we want to track for these abilities and I'm gonna do that for let's see the discussion for sure so you literally just type resource fly and I believe we'll do this for channel cool and I don't know if we do it for reply and then we don't and then for the user we have to type rola Phi which is there it gets added by default when you run that generation great so you would basically put resource of fly under all the models that you want to have the ability do its work on so if you want to admin something you put this there so what's next is to basically create an admin user and you can't really do that with the interface currently I think you could if you wanted to really set up devise to be such a thing but the way I'm going to do it is in the rails console and it's really defining an admin user kind of trivial so it's not so much that you need to do it often you know so I wouldn't even really stress over an interface if you're comfortable using rails console to do so so I'm going to find user find and we would look for the user you want the admin to basically be actually just find user and it's gonna do this crap and look for all the users and my ID person I want is for this email and it's gonna be ideal ones so we can do at user equals find or user or we could just put at user equals user defined and then pass in one and should spit back that only user there we go alright so then we have that available to us just as user and clean this up so that user is that so now we need to do at user dot add role since we have this available to us now and then just type a space and then in quotes put add and then hit enter and you'll see all this stuff happen and it's basically just saying I'm an admin now so that's kind of cool and if you already just want to do some trivial stuff you just put it user dot save and if you want to see all the roles can do at user dot roles on that user and you'll see that there's one for admin which is my ID so if that makes sense hopefully it does so that's pretty much all you do to set your own account as an admin so we can hit control D to get out of this and now my account has admin rights so if I want to go back to my account and edit it I should be able to do so but the only thing is stopping us is that I created a handy helper to check for this stuff so I'm gonna go into my helpers again and go into the application helper and it's a very top under the require statement I'm gonna create a new one because I want to have the access to this entire throughout the entire app I'm gonna do one call role or has role and then pass in the role hands roll so we're just basically calling what's on that guide I'm just making a helper out of it so I don't have to type this in each of my views and this is extendable for the sake of you could pass in any kind of role here if you want more than one here you can add one so you can do like else with Ruby that's what else if looks like it's weird I know but then you can just add one that says you know like user that has rule and then moderator or something you could do that but I'm gonna leave it as is so we've got our helper then and this should kind of sum up this view so now it displays and it shows that hey I am currently an admin so kick-ass so last but not least is to make sure this button is hidden so when we go back into our views into our sidebar and I put this comment here so we can do an admin role check that well that should do the trick so if we do if has role question mark and then we just type in that colon admin should get this to display the way we want so since I'm an admin it's not gonna change anything but if I were to log out and log in as Jace Minnie it's not their cool all right so since Jay Smitty authored this one noticed the buttons are present he didn't author this one so I'm gonna sign out again sign-in as my admin account and you'll notice okay since I altered this one I can edit this one that makes sense but I want to be able to author Jay Smitty's as well so we can go back into the views where we did that check initially and updated it accordingly so we'll do in our shared file wherever that check is for the user I could do or as role admin and boom so I can edit that now sweet so cool so I can delete that I can edit it I can do a new channel and do all that stuff the same is true for creating a new channel so say I could probably just go to channels and show you though oops so here I'm gonna fix this up to where I have access to this I won't see this but if someone else goes and accesses it they won't have access to any of it so let's go ahead and go to our channels or index and we can uncomment this now great and then I could probably just throw it Aztec singer something like so let me get rid of this comments so great so that text is gone if I were to log out and go to channels boom sorry you don't have access to this page cool great so I think we're taking shape login once more and I could extend this further so say on my comments it makes sense that I can edit them but I want to you know edit Jay Smitty's too so let's do that for the replies where we at if reply author we could do the same thing here so has roll question mark colon admin I can edit this awesome great so that is the finishing part of the admin level stuff up next I'm gonna tackle these URLs because you notice there's a two there and I want it to actually say like design thinking or first discussion when we go into the actual path so it's way easier to share it with your friends or just link to it on social media or something like that so that will be the topic of the very next video and probably round out this short course short series whatever you want to call it I'll talk to you in the very next video you
Info
Channel: Web-Crunch
Views: 4,464
Rating: 5 out of 5
Keywords: ruby on rails, rails, rails 5, forum, coding, coding tutorials, learn to code, coding rails, Let's Build: With Ruby On Rails, web development, web development tutorials, screencasts, programming, programming tutorials, web design, bulma, gem, ruby, html, css, sass, scss, Let's Build: With Ruby on Rails - Discussion Forum - Creating Admin Roles - Part 8, admin
Id: PmkBBRq6jQ0
Channel Id: undefined
Length: 14min 38sec (878 seconds)
Published: Mon Feb 05 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.