Codeplace | Build a Ruby on Rails Admin Panel using rails_admin gem

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome to another code cast and this time I'm going to teach you how to build your own admin panel well it's not a really your own since I'm going to use a gem which is called Rails admin but it's the fastest way and and of course the easiest way to create a full feature rails a an admin panel full feature admin panel you're going to see it's very very powerful I'm going to show you the apps so that you can see what you are going to build let me okay so first thing is just a login button because it makes no sense to use the app if you're not logged in so I already have an account set up ok so it if you didn't watch the my previous tutorial called Ajax crud I really encourage you to do that since we are going to build this one on top of that ok so so there can be a little faster so please go back and see the Ajax crud if you want or or you can build a app from from scratch but if you want to do the exact same app please go back and see the air the the ajax crud tutorial so we are now logged in and we have here three three records that if you type in the URL admin see what happens you see how nice it is and this is completely out of the box I did I I did not I haven't made any customization at all this is right out of the box so this is I'm going to teach you how to install the chair and do this basic setup but this is going to be all generated for you so as you can see I'm going to just do a quick demo you have a dashboard when you can see all the models that we have in our case we just have two we have the posts and the users of course the amount of records in on each and you can then list show you the list you can see like a database you can see all the records there let me go to the dashboard again then you have the the add new you can add a new a new record to that model and you will also have the ability to export as you can see here you can export to s CSV JSON or XML and this is all out of the box this is all done for you just installing in do a to commands and you'll have this set up for you so let's begin let me just start stop the server and go to our admin folder by the way this folder here is the one that I'm going to use this is just a copy exact copy of the Ajax crud app I just changed the name and we're going to build on top of this so you can do exactly the same thing so let me go there so let's go see deep backwards and now desktop CD admin and we are there let's just do a quick check on on the app see what we have to start off okay cool we have no login system and if I click admin no rachis match so we do not have any admin panel set up as you can see you can you can do posts here and you can edit let's see you can destroy them simple simple Ajax crud actually so first let me clear this let's generate the controller for the landing page so that can have we can add that button of the login so rails G controller home and an index page just an index page actually I'm going to open device here on the getting started section since the six second step we're going to make ok so just just taking a little while let me just now it's setup let's install device actually first of all let's open our app so desktop admin cool now the first thing it's to add the device gem ok we are cool now run the bundle okay now just let me check on the on the installation because I never know by and uh here we are rails generate device install you can paste it ok cool no errors and the last command is to create a model so we're going in our case is going to be a user mode so we could indeed and we could in fact now rate and run the Reg DB migrate but since we are actually building an admin panel I only want to admin users to be able to access the admin panel so for that I'm going to add an extra field on the users table it's going to be of a boolean type through or false and it's going to be named admin ok now we are ready to break the be migrated ok everything is working fine now you can actually do is I go to AB controllers let's do some things now at the first let's create the route route so we can delete this get and leave the route instead of posts is home okay so the routes we have all the route set up now let's go to the post controller and we want to add the before action from device authenticate user okay okay now close this and now in the home controller we want to add a condition here so if current user so if we are logged in then re-direct too and let's see what we have actually in here because I want to actually see to the post path so if current user we want to be redirected to the post path okay okay here we are only if the current user exists or if we are logged in now let's go to our application dot HTML and what we are going to do here is very simply I already have the code actually on my previous app so I can go there is going to be much faster just a copy copy and paste this okay so this is a very simple if-else statement it you already seen I'm sure so if the current user so if the cur the user is locked then give me the link to log out if it's not like then then give me the link to login this is a this is very simple so um I think we can close this now let's let's just start the server and see what we have so far so refresh okay see what happened now I am at the home index page and I have a button to login if I press again if I actually put posts you see the before action authenticate user is working so it's redirects me to the login let's create a user so this is password is going to be password as always okay and it's done and we have the link to logout it means that it's working let's do the classic delay hello world create okay everything is working let me log out okay perfect let's just delete the text there we don't need on the index view I can delete it okay okay cool now we are actually ready to start and implement the admin panel I can just refresh it if it's ok everything is working fine so first of all we should put of course the gem in our gem file and to do that we can go to the github on this URL as very can the rails admin this is the official heat-up repository we scroll down here I'm going to see the installation so the first of course is to bundle the gem so let me go here let's go to our gem file call and type rails and the score admin then server and run bundle install ok it's done now the other command that you have to do is of course generate the the admin ok so you can copy and paste this command here go to your terminal paste it press enter it's going to prompt if you really want to mount rails admin you just press ENTER and eat that so it creates a route for you that the slash main pen this is the the you are eligible there and an initializer rails admin dot RB cool so let's go to our application we can close this and if you see in our let's go to roust let's like and show you you have a new route here mount rails admin engine to admin ok this is all set for you and you have an initializer generator as well and here we are you don't have to mess with anything here so we can actually test it already let's do rails s let me refresh the page ok perfect let's do a login with the first user ok everything is working and now let's try and go ahead to the admin panel it's just taking a little while since the first time ok and you see I mean how how easy was it it's just two commands you just install the gem and then you install the the rails admin and you have this right out from the box how cool is that No ok but now to before we finish this video I'm going to teach you how to actually take advantage of the admin feel that we add to the users cuz right this moment any user actually let's create a second user any user should be able to to access the admin panel so I'm now I'm user 2 let's go there and let's see what happens you can see I can also go to the totally admin so the way we can prevent this to happen is if you go here to them to the github official page if you scroll down you will have the human link in here you have a bunch load of information by the way the detox for this gem are awesome they are really really well explained and I mean there is nothing missing if you go here on the right side to authorization it's not authentication but authorization you have here this config here which basically tells you to redirect to the main app root path unless wall worden user is admin so and left the user as admin is going to redirect you to the root path so just copy this config here go to your rails admin initializer okay I mean the initializer folder rails admin and just paste this can actually paste it right in here and we're going to do some changes so instead of doing a method here because this expects a math a created method called is admin and we do not have this method be gonna you're going to do with some some just a little different so we're going to actually go to the field from the admin field of the user and we're going to check if it's true okay now we can actually go to the rails console are we going to add true to the first user so we're going to set the first user to admin so you equals user dot first okay here we are the first user so dot add me you're going to see Neil you see now let's put a you dot admin equals true I put you dot save okay now if I type the year you can see here that the first user is admin and actually let's check to to see that the last one is not so the first one is true and the left the second one is not so let's refresh the server actually let's restart the server since we made some changes on an initializer let's restart the server for safety reasons so let's go to the the page refresh this log out okay let's log in let's look in with the second one the one that I that I know that is not an admin okay so I'm logged in and see what happens when I press add mean it redirect me to the post and I can type admin as long as I want I'm not an admin user I'm not able to access the admin panel now if I log in with the first one and this one is an admin so it should redirect me to the admin panel and here we go you see how is it was very cool very cool this gem indeed in very powerful easy to implement two commands just install the gem and then you were just install command and you are all set up so this is it and I hope you have enjoyed it and then see you again in another step cast
Info
Channel: Codeplace
Views: 60,430
Rating: undefined out of 5
Keywords: codecast, ruby on rails, rails, stuk, stuk.io, admin, panel, rails_admin, gem, settings
Id: agzm_O-pZFE
Channel Id: undefined
Length: 16min 54sec (1014 seconds)
Published: Wed Apr 01 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.