Rails Admin Interfaces with ActiveAdmin

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] guys this episode we're talking about the active admin gem and how to use it to build an admin interface for your rails app so this gem is rather interesting it relies heavily upon either auto-generated code or dsls to customize the admin interface so this is interesting it uses devise it creates an admin user model so all of your regular users are very separated out from active admin which is very nice for security reasons there's no chance that anybody could ever you know set their role to admin and gain access to it because they physically wouldn't have it record in the admin user table so that is nice you can customize this so that you can use roles and the same user table if you want but this will look at your registered models and add them to the admin interface and it will generate index pages search pages or search filters it will generate show pages edit forms all of that stuff automatically for you so long as you just register the model in the admin you can even tell it to use and pull out specific scopes from your models to display and allow for filtering there's all kinds of steps that you can do with it and it has its own DSL for customizing your forms your ransack filters your scopes your views even and adding buttons to the show and adding actions you've got a massive DSL and that's one of the pros and the cons with using this gem it's one really easy to customize things a little bit but number two it's kind of very hard to customize things a whole lot because you have to do it all within their own well their own DSL and so that gives you pros and cons but let's dive into it and try it out and take a look at what we're we're working with so one cool thing is that we need to just register our models and we can go out our scopes filters and customized our index and is few lines of code as that is we don't have to worry about any model stuff no controller stuff no views all of that happens inside our admin full file for our products so that's kind of neat let's dive into the gem itself I'm going to use the version that's on github the latest version that's stabled you can use the master branch but there is possibility that that will break as they're actively working on it so let's create a new rails app called admin example and we'll open this up in our editor and go to the gem file paste an active admin in there and we also need to grab inherited resources from rails 5 as this gem is been deprecated and then taken over by the active admin team and last but not least I actually had to specify devise manual in here to get the generator to run so that may or may not be fixed later on as you watch this but at the time of recording I did have to manually special specify devise so we can run bundle to install that and once this is done we can run rails generate active admin install to install it so rails generate active admin : install well setup our admin user devise model will setup configs for at active admin in our initializers folder create routes for active admin and an app admin folder where we create all of our admin sections in there oh here we go this gives us all those files we can run rake DB migrate in order to add the admin user model we can also run rake DB seed in order to create our admin for the admin area so if you open up seeds RB you can see that it added a line for creating an active admin user and it's just admin of example.com and the password of password so we'll run that as well it also created active admin comments so that you could comment on any model in the admin period their polymorphic so they can be applied to anything and lastly let's generate two scaffolds so we can play with those so we'll have user with a name and an email and we'll have rails generated scaffold post with a title a body is text a published at date/time and we'll also have a user references so this way we can see how it works with associations so let's run write DB migrate create those and let's start up our rails server and see if we can login to localhost 3000 slash admin after a second attempt I got it to work I actually ran into this issue with jQuery UI datepicker and had to use the active admin from the master branch at github in order to make this work so they're in the process of re-releasing an updated version of the gem they're working on version 1.0 for official full rails 5 support but it does work perfectly fine in rails 5 if you use the master branch so use at your own risk I guess and we can log in here and we will have our dashboard this comes from the configuration of app admin dashboard and this is the block that it uses to generate that container so if we deleted this we'll see this block disappear and there you go so this is actually generating the views inside of a ruby file which seems a little weird so these div spans small tags are all coming from a active admin dependency called arbre you can find out a little bit about this and their documentation and take a look at our bure components this is the section for that it shows a little bit about how to do that it comes with some cool stuff like panels that you can use and columns to split up your stuff similar to how you would with bootstrap you're just using Ruby to design your views instead so it's kind of interesting that they chose that route but the benefit is you can make very quick tweaks to it without having to dive into all your views and have a lot of files to to define your admin with so they've opted for this route it has its benefits and its drawbacks but it does work and this is our admin user section we can see that you get all the admin users listed out in a table you can filter them you can type in whatever you want and that will automatically search for you and tell you which fields you searched upon but you'll notice that our models don't exist in here just yet so the reason for that is we have to add them to files in admin and we have to register them with active admin so let's go do that if we go into our app and we see a rails generate active admin resource we can pass in the name user and post and this will generate two empty files for us that register user and post models in order to display them so this is great except these have to be customized to pass in the permitted parameters it's not going to generate those for you although I kind of wish it would but here we can say name and email and then for post we can permit params what do we have title body published at and it user ID so we had those columns there and now we should be able to refresh and see our posts and our users and if we created the user Chris Chris ago rails we can see that I have a page age it worked it's kind of like admin scaffolds pretty much with some nice reviews but you'll notice here that it is smart enough to understand there's an association for the user ID column and it gives me a drop-down for that instead so we can add a title and a body and if we fill out the published at that would of course fill it out but let's leave it out and create a button up here to add that in so that we can have a publish in the unpublished button now the way that this works is that we can have a member action here which is kind of like defining a route and we could have a publish one with the method of put and this is very similar to going into your routes files and saying do member put publish it's almost exactly the same except that you're defining it inside of active admin instead so let's move those up here organize that so that is how you would define the route and the block that you pass it is actually the code for the action so you say a post up find params ID post that update published at time not zone dot Nell and then redirect to the admin post path and pass in the post and that is as simple as it is but we needed to display on that show link so here we can say action would they call it so you can find all this stuff in the action items so action item publish do and this is a customizable for the show action or the index or whatever and each of these is one of the commands in the DSL that you can use to add links to your views so if we say only show we can add a link to publish at publish and minute post path and it will automatically load up the post variable for us and we want method of put to match and we will only want to do this if post that published at is not a thing so we can refresh and now you'll see the publish button clicking that will hit our little block that we wrote and mark publish that as the current time so that's really neat that we can add that without touching views or writing controllers or adding routes kind of all do that in this one file and we can do the same thing if we duplicate this we can have unpublished for the unpublished route and we'll do this if it is published and we can copy this one and add our own unpublished method in here that sets it to nil instead so now if we refresh we have the ability to unpublish and i spelled that wrong and that sets it to nil and it shows up as empty and we can publish and that button will change and that's as easy as it is to add in an action like that into the UI so that's pretty cool we're using some of the DSL for that it's starting to mix a little bit of our views as well as a routes and our actions our controller actions into this one file though so you might notice that that gets a little confusing um and this is kind of the main drawback of using active admin once this gets about 10 times longer and you have a lot more complex admin interactions this gets a little frustrating to work with but at the beginning the easy ability for you to add in these actions and stuff is very very nice because the speed of development can can be very quick there one of the things that I want to point out here is that it interprets that user idea into the Association and makes a clickable link for us to you navigate between post and their authors which is very cool so it's smart enough to know to use those associations to build out that admin UI I like that a lot the other thing we can do is we can go into the post model and we can say scope published and published ones are we're not published at is now but unpublished ones are where it is nil and if we add those in we can go to the top year Intel active admin we want to display those scopes unpublished and all of course is a scope that always comes with every application record or active record model um so going back to posts we can scope these and say well there's zero published ones there's one unpublished one if we go to view it we click publish and go back there is now one published record and zero unpublished records this is pretty neat that you're able to add in scopes that quickly you don't have to go and customize views or anything you can just say scope this scope that and that's it another thing you might want to do is customize the form so for example with devise you have an encrypted password column in the database but you obviously don't want that to ever be in your forms so you want to be able to customize it well this is really nice because you can say a form do F and you can say inputs details do and just put in your input so you can say let's have one for a user one for the title one for the body and it will go and say okay this is a user Association so let's create a drop-down and let's load up the users and populate the drop-down with them then the title is a string so let's create a text field but body is a text column so let's create a text area for that one and then at the very end we can say actions which will update our form to say up post and cancel and before you know it you have a form that's customized exactly how you want it now there's no way I can cover everything about active admin in an episode or even really a series but active admin has a really good documentation site where you can learn about arbre the Rubeus syntax so that you can use that DSL for to create your views like the forum stuff or the show actions or any of that stuff you can use that to build out your views in the admin a little bit more you can look up how to create custom pages as you see here you can design your indexes as blogs or grids or tables or whatever you would like they have all kinds of different things like building your custom controller actions batching actions which are on the home page so if you go to one of these you can check all of these off and say delete all of them or maybe you want to mark 10 posts as published all at once you can see how they recommend you doing that they also have information on how to set up pundit to enforce permissions across different user roles all kinds of different things it's been used by so many people you will always be able to kind of find a good example of how to implement whatever feature you're trying to implement into active admin so that's a quick introduction to it I hope you enjoyed it active admin has been in use by me over the years so many times that it's definitely been worth its value there's been frustrations around how to organize those admin files well but you can always figure out how to pull stuff out into concerns or something that you would include if you really needed it to but I found that if you just organize these and good chunks and you keep all of them like action items together and member actions together and keep it consistent it's really not too hard to manage even if this gets to be three or four hundred lines code so with that said I hope you enjoyed this and I will talk to you later [Music]
Info
Channel: GoRails
Views: 44,824
Rating: 4.9704251 out of 5
Keywords: Ruby, Ruby on Rails, Rails, Javascript, HTML, CSS, Servers, Databases
Id: NJYtzznKrg0
Channel Id: undefined
Length: 17min 42sec (1062 seconds)
Published: Wed Dec 28 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.