AWS+Rails: Devise Profile by User Role

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
do you want to learn how to create a dynamic profile page by user role then stick around and find out how I'm Thomas with brain chest digital full-stack developer obsessed with learning if you interested in learning full stack development please consider subscribing below in this 80's real tutorial we're do something a bit different we got an awesome request from a user that'll put up on the screen now Jorge was looking for how to create a separate profile page where dynamically display the profile page by user role the example Jorge gives is of a marketplace with buyers and sellers showing different profiles for each user type in our case 2 applies to the ADA base rails application I'm going to add roles and then populate them with a default moderator and contributor role then we display a different profile page depending on the logged in users role this was a really fun video for me to create and I thought it was a great request so I hope that this answer really helps you out and help some other people out on an interesting technique I used to accomplish this the technique I'm gonna be using is not one that I created but rather one I learned from watching Ruby on Rails tutorials from Rails cast and go rails and some of the people I watched while I was trying to learn and actually still watch to this day so I definitely I didn't create this technique but rather I just think this is an interesting use case for the technique that I learned long ago from these other creators with that being said let's get into the a TBS rails tutorial as you can see we have our AWS rails application running locally so the first thing we're gonna do is switch over to the terminal we're gonna generate our role model here you can see we're generating a new model I call that role and we're gonna give it a name in a code we use the name for the front-end presentation and then we reuse the code on the backend to figure out which template we'd like to display since both of these columns will just be strings we don't need to pass in the data type so we go ahead and view that migration really quickly as you can see it's just like you'd expect we're creating the rolls table or giving a string of name as well as a string of code and the timestamps let's go ahead and run that database migration now rails DB migrate while we're here we should actually also add the role ID column to our users table rails G migration add role ID to users when add this as a references to add a foreign key for us here is the role ID on users migration we're to make one quick change here we're gonna allow this to be no since we already have users in place otherwise you're not gonna be able to get to Smet migrations through now we can go ahead and migrate the database again next we're gonna flip over their application and write some tasks to populate our roles adding roles in this case is merely set up to be able to show the different method to display or dynamic profiles we're not gonna have a front-end interface at this time for our roles so instead we're gonna populate those via tasks I've already showed how to populate data via the seeds file so I thought maybe it'd be nice to show a different method and a couple of practices that I use there so we're go ahead and click new file I'm gonna save this right away then we'll just call it one-off rake in this case we're gonna have a few so just go ahead and paste them in and then we can walk through it first task here is populate roles what this does is looks for a role by the code in this case default and then either grabs the first by that code or creates a new instance once this instance is found or created then it updates this instance with name as the capitalized default we repeat this process for moderator and contributor I found this to be a pretty clean way to write your tasks so you can easily run this task many times without issue if at a later date you want to add more data to the task this is completely fine say in the future we added position and then populate it for each value you could just run this task again and as long as your codes have not changed this would be completely fine this is part of the reason why I like to split out code in name this allows for a lot of nice benefits like this while even though they're very similar for example you could translate name two different languages while leaving code the same next we're setting some defaults so the first thing we're doing is grabbing and cashing the new rules we just created when I grabbed default role instead we're gonna run a fine by instead of a where so we have a single instance this is effectively the same as roll dot we're code equals default dot first just a little bit cleaner way to present that the next line here rolls through all users and updates them to our default role then we call out to specific users bear and make him a moderator and then myself and make me a contributor this final task is something that's a little bit useful in this particular case it's a bit overkill but it's something that's really nice if you end up having a lot of tasks as part of a feature deploy this is simply a task that runs other tasks so instead of having to call these two tasks populate roles and default rules after we deploy we can just call roles underscore all and we'll just run this series of tasks in order well it's a bit overkill here since we only have two tasks you can imagine if you had ten or fifteen tasks as part of a large deployment that would be really useful and really quick to only have a single task to run to get everything in line in production flip back over to the console and run our task there we go it looks like that completed now that that's complete that takes care of all of the current users but what about new users let's flip back over to our application and handle that now first we need to jump into the models and add the new relationships so in role we're gonna add a has many users make sure users plural there then in user gonna say belongs to role in this case we have to pass this is optional true we first allowed this to be optional at the database level by changing the no option to false in the add reference migration here we're doing it at the model level as well as we'll actually be adding the default role after the user signs up to accomplish this we're going to add a block down here in the private section or to create a new method set to fall roll I set this method to update the roll ID to the roll with the code default we're gonna go ahead and call this with an after create hook finally since device manages our users we need to add some code to the app controller to allow for this new roll ID to be accepted as one of these sign-up parameters this code is taken directly from the devise github gems documentation what we're doing is creating a new method called config permitted parameters we're permitting on signup the new key roll underscore ID then we call this method in a before action if we are accessing a device controller I'll include a link to the devise documentation in the description below in case you want more information if we flip back over the front-end application we go ahead and test this out so we're gonna click sign up we're gonna create a new user Chelsea my wife at example.com and we'll go ahead and give this user a password and then click sign up for now we're not gonna save this password is this only a temporary like a test user but you can see we've successfully signed up so now let's flip back over to the terminal we'll open up the rails console with rails C and then we'll grab that last user here you can see there's our new user Chelsea if we then grab that last user's role early set with the default role now let's go ahead and move on to our dynamic profiles we can flip back over to our application and start to work on our profile page so here in our application you can see the user show page which is our current profile on the front end you can see this page by clicking on the logged in user in this case our example user Chelsea you can also click on other users profiles and see their pages like my page or Bears page in this case Chelsea bear and I are all different user roles we can quickly demonstrate that by putting out the user role to the front and website just above the bio we can add a block of code to do so now we'll open up your be tags and then output the users role dot name and if you refresh the page you can see that bear is a moderator Chelsea is the default user type and I am in the contributor role to set this up first let's extract all of this common code into a partial we'll grab everything we'd like to be dynamic by user role which is effectively the entire profile section here and we'll cut that they're going to create a new folder in users called profile this will just give us a nice clean way to organize all of our various profile types within the profile folder we're gonna go ahead and create a default partial we will paste in the content that we just cut from our show page we'll go ahead and render that partial in the show page and if we reload the page you can see we just named our partial incorrectly we forgot the leading underscore let's go ahead and add that really quickly now and then refresh once more and there you go the page works again as you can see for all of our different user types now let's go ahead and make this dynamic by user type if you flip back over to our users show page you can see one method for making this page dynamic by role you can replace this partial inclusion with a series of conditionals when you split out the user by type and then render a partial accordingly as you can see this can get really sloppy really quickly building an ever-increasing conditional chain as you continue to add user roles instead we're gonna use a method in rails paired with a clever technique that I learned from watching two of my favorite Ruby on Rails tutorial creators but we'll get to those in a minute first on the rails side of things we're going to use the lookup context action view helper specifically the template exists method if we click on the link now as you can see this method is looking for the template name that we want to find as well as the prefix whether or not this is a partial some keys and some extra options we're gonna go ahead and use this method to dynamically preset our profile partials and flip back over to the code first thing we can do is wipe out our current code and replace it with our new lookup context and walk through it I first saw usage of the lookup context technique in a rails cast video unfortunately they stopped creating new videos about seven years ago then it came go rails for me go kind of picked up where rails cast left off providing extremely useful feature based tutorials this is not an ad I'm just a big fan so if you're trying to learn Ruby on Rails I'd highly recommend that you subscribe to go real seriously pause the video go to go rails comm sign up you'll thank me later with that on the way let's walk through the technique were using here as you can see we're performing a conditional against the lookup context to see if the template exists the template we're checking for is the role code of the current user when I pass in the users path and then finally we're gonna say true meaning that this is a partial this is gonna look for a partial at users slash profile slash the current users role code if that partial exists then it will render that partial but pass in the user that we're currently viewing if not then we're just gonna display our default partial the functionality that this gives you is that me as a contributor could have a different view of bears profile than Chelsey as a default user one last point I wanted to make when looking at how I'm using this technique in my case it makes more sense that the current user gets to see more privileged information or maybe have more ability for actions whereas in the requested example it would make more sense for the instance user in both cases so that no matter who's logged in looking at the page the buyers profile always looks like a buyer profile design and the seller profile always looks like a seller profiles design I'm gonna back out those changes but just want to point that DIF out because you really want to hone in and pay attention to what you place in those two positions so if we save this and go ahead and refresh our page you can see that we're still correctly or rendering the user profile so now let's go ahead and make some changes for the different role types to do so all we need to do is add new partials into this user's profile folder 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 our we have a limited time offer or co-worker here bare perform one trip oh my gosh we're going up there so here let's copy the default template and use that as a base we're gonna create a different view for our moderators first we have to name this identically to the user role code in our case that's moderator all lowercase HTML GRB don't forget to lead with an underscore go ahead and say that and then paste in our code in the case of our moderators maybe we'll add an ability that doesn't really exist to be able to warn a user if their profile contains something in appropriate again this doesn't actually do anything but just from a demonstration standpoint so you can see that when we log in as a moderator in a few minutes that this user will have these additional items in the profile pages as they view them next let's go ahead and add one for our contributors as well again you're say underscore and then the user role code dot HTML body RB we're in go ahead and save that again will copy the moderator this time as the starting point here we'll add a few more fake links to lock and unlock user accounts again these don't actually do anything just more to show an example of what you could do the power here is that you could have completely different views depending on the user that's logged in if we flip back over to the front-end application here you can see my profile when logged in as Chelsea the default user nothing changes here this should look exactly like it did before then if we flip over to a new tab where I'm logged in as bear on the same profile you can see the addition of this fake worn user link that we added to the moderator role finally if you look at my own profile logged in the contributor role you have the additional thing lock and unlock user account buttons obviously these fake links are a super simple example but you can really get creative with how you use these dynamic profile pages so the same page can look very different for very different types of users I mentioned previously examples where I learned this technique from rails caste and gorillas they both use this technique for notification system where you present different partials by event type so I just want to throw this out there as a different way to use this type of dick technique while also answering Jorge's question I personally like to use this technique for product pages as well maybe you have several products categorized in a few different product types and the various types lend to being better displayed and specific layouts I hope you found this helpful I find this to be a really powerful and useful technique if you did find benefit here I've really appreciated if you'd consider liking and subscribing the video and with that I'll catch you in the next two ABS rails tutorial
Info
Channel: BrainTrust Digital
Views: 1,787
Rating: undefined out of 5
Keywords: Devise Profile by User Role, devise rails, devise user, aws rails, ruby on rails aws, aws ruby on rails tutorial, user profile page, rails 6 tutorial, rails 6 devise, ruby on rails tutorial, subscriber question, web development tutorial, web development 2020, web development, devise gem ruby on rails, devise gem ruby, devise gem authentication, devise gem in rails, devise gem rails tutorial, devise gem in rails 6, devise gem rails example, ruby on rails, Full stack
Id: wbRDqZCchs0
Channel Id: undefined
Length: 17min 8sec (1028 seconds)
Published: Sun Jul 12 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.