- Read Tutorial
- Watch Guide Video
James speaking
So. Right here I've just got my text editor, I'm using Visual Studio, open and we will go ahead and go into the correct directory. Let's call this rails_generators
.
OK so one of the first ones, the main generator that we're going to run, is actually a rails new
. So this is something that everyone should be familiar with when you're starting a Rails application. Although it's not commonly referred to as a generator, it is technically a generator. It's calling the code and then running all the templates all the files for new
. So we'll go ahead and run this. I'll just call this MyGenerators
and we'll exclude the tests and then we'll call --database="postgresql"
And once you get started inside of this. Let's make sure we're spelling postgresql
correctly.
rails new MyGenerators -T --database="postgresql"
There we go. So I'll maximize this. So what we'll do then is yes you can see right here the rails new
created all of these files. So it creates a README
, a gitignore
. It creates all the basics: assets, views, just the bare bones version of a Rails application. So this is something that everyone should be fairly familiar with.
So we'll go ahead and clear
that.
Then we'll cd
into the application.
We're actually going to bring up. So is there anyone on that's on right now want to kind of briefly explain what their understanding of a rails generator is? Does anyone have questions about what a rail generator is?
No one speaking up. So we'll go ahead and do just so I can kind of show you guys the really neat features that rails has built in when you actually call rails g
or rails generator, you can then call this -h
or you can even do --help
. They're virtually the same thing, but when you run it, rails will automatically print out the basics of everything that you can run.
Right here we'll see inside of rails. We have a generator that's assets, channels, controller, generator, helper and so forth so. The primary ones that you're familiar going to be familiar with is a model, a resource, and a scaffold. So I'll go through each one of those and we can kind of go over the differences between each one.
The first one and big one is the rails scaffold. And this is one that everyone is familiar with. It's the very first one you're running the devcampportfolio application. We'll go ahead and run that. So to run a generator you can to do the rails g
and then the type of generator you want to run.
I'll just call this Article
for now. Because it's a scaffold we can go ahead and also give it some additional arguments for the table settings and what the table columns are. We'll call this title:string body:text
.
rails g scaffold Article title:string body:text
So now the neat thing again is once again it's printed out all of the different files it has created. Scaffold is very heavy. It's created the migration file. It's created the models file. It's neat because you can actually see here that it's running the resources for articles; that it's running the controller, all the views in an ERB format, the different helper for articles, any additional builders and the assets for the coffee script style CSS, and also because we ran a scaffold for the first time it created the scaffold CSS.
So now as you dive into this a little further we can kind of see right here created the article SCSS
the scaffold SCSS with the basic styling.
So to go ahead and get started let's go ahead and we'll just open up our rail server and see what runs. First, since this was the first one, we'll need to run the db:create
. So it can create the database for us.
rails db:create
Then we'll also run a migrate.
rails db:migrate
So now we run our rails s
, it'll start our server (localhost:3000
) and it's the main screen, "Yay! You're on Rails!"
You could change this if you wanted to inside your routes, but let's go ahead and come over to articles(localhost:3000/articles
).
So again this is the default templates that the scaffolds ran. As you do this, it's obviously already created all our views, including the creation of the forms and everything you need. As we dive into this a little further, with the website open, we can kind of reverse engineer some of this.
So first off, we have our articles
so this is going to be our index page
so we can go here, views
, articles
, index
.
So this is this right here is what our scaffold generated. Just super quickly for us. As you go here to the new article
.
We can come here, determine that it's pulling from the new.html.erb
template which is also calling this partial for form.
So as you can see a scaffold is very heavy. It created a lot of content for us, a lot of additional code that you might not necessarily use. So most developers, if you run a Google search about rail scaffolds, most professional developers don't often use scaffolds. However, they are quick and easy.
If you need something up just in an instant, as you can see we quickly create that scaffold. There's also some really neat things that we can get into later on, towards the end of this webinar that I'll cover for just customizing and editing scaffolds or all generators just to give you a little bit more custom look and feel for your generators.
So with this scaffold created let's go ahead and keep moving on. So we'll close out of our server and then next let's just go back to look at the different type of generators. We'll do rails g -h
for help. So we ran the scaffold
. Now another common one is this resource
. Let's go ahead and just run a resource generator. We'll do the rails g
and then type in resource
. And this time let's call this Blog
. And we'll call this title:string
. Let's do something else, let's call this content:text
rails g resource Blog title:string content:text
So I just name this content
so you see that it doesn't always have to be body:text
. It can really be customized whatever you're building. We use a resource generator inside of your rails devcampportfolio
for the portfolio portio
n of it and let's go ahead and kind of look at the differences that has created so far.
So as we look at this a created a migration
file again. It created the models
files for blog.rb
. It created the controller
, the views
. It created just a directory
, and then it created the helper
, the coffee script
, and the stylesheet for blogs
as well as his set up a route
for us.
Let's go ahead and let's look at some of the differences. So if we come to the controller. Let's first look at the Articles Controller(articles_controller.rb
) and then let's look at the Blogs Controller(blogs_controller.rb
)
So you can see right here that the scaffold generated all of this content for us automatically. It built out our entire CRUD functionality for us. Whereas in the blogs Controller
it just left us an empty template for this class of blog(class BlogsController
).
You can see the same thing models. They're both going to be blank(article.rb
& blog.rb
), because we haven't set up any
rules or anything for the class of article(class Article
) or class of blog(class Blog
)
You can see the main difference there are a lot of additional code. So this json
unless you're necessarily trying to run an API
immediately you're not going to need it. So this is a lot of excess code that we can now take out as we're custom building.
It's our our resource generator. So we won't go into that right now, but does anyone have questions about the differences between a scaffold and a resource controller up to this point?
Okay. So I will quickly just run the rails server(rails s
) again. This time what you'll see is as we go into blogs.
Before I go into this, will it or will it not work? Who knows what's going to happen when we go to this new link?
Student speaking
It's not going to work because it's not been set up.
James speaking
Correct. So there is an additional error that I didn't expect.
So we did have to run the rails migration(rails db:migrate
).
So with that ran, let's go ahead and restart the server(rails s
). So who can identify what this error is?
Student speaking
It can't find the view.
James speaking
That is correct. There is a couple of things we should do there. def index
. And for now, we don't necessarily need to call anything.
And then we just go to our views
and let's quickly create an index.html.erb
.
It should be. [00:13:25][1.6]
[00:13:28] Pay. So now will it work. [00:13:34][5.6]
[00:13:41] If growing up won't work. OK. So you're right. Someone someone said don't bring up a blank page and that's exactly correct. So because we don't have any content available to the index page and the controller or anything on the index page we did just get a blank page but now we're not running any errors but if you continue to run in to these templates so knew you would continue to run into those temp letters. So again that's just a major difference between the scaffold and the resource generator. Just the amount of content you're going to have pre-built in. So if we wanted this blog to work we can now manually build out the correct functionality. So what. Will clear out to us. Does anyone else want to go over a specific generator before I go onto one of the other ones. [00:14:39][57.7]
[00:14:44] So we've got the views here. [00:14:45][0.9]
[00:14:52] So again there's there's a lot that you can go over so let's go ahead since we don't have anyone asking for anything specific on this front. Our next one which I was planning on doing which is a controller. [00:15:02][9.9]
[00:15:11] So we'll go ahead and give this Mayme up on this one a name and we'll just call a post so let's do this first before I get into that I will run because this is another neat thing just like you can call the rails G dash dash hell we can call a specific Mike ration or reste generator and have rails give us information about that too. So as we go to rails chief controller dash dash gives us all of this information. So it gives us some of the options that are available some of the features and then it also gives us a neat little description. So okay now all Clake isn't a little annoying. [00:15:57][45.1]
[00:16:01] Okay. There we go. So the description stubs out a new controller and its use pass a controller name either camel cased or underscored and a list of views as arguments to create a controller with a module specify the controller name as a path like parent module control names. So it really breaks it down easy we can even use a specific example now as we generate it. So this is going so we'll type rills and you can and again you can do generate type out the whole word or just g so will now call the type of generator and credit cards. Open credit inclose so base off the description. Who can tell me what this generator's going to create for us. [00:16:49][48.2]
[00:16:56] Anyone have any ideas. [00:16:56][0.8]
[00:17:00] Maybe more functions build out of it. Yeah [00:17:09][8.3]
[00:17:09] so it will generate the controller and then it will build out the means. [00:17:13][3.7]
[00:17:14] So. So what we'll get is we'll get the controller and then the controller will have the pre-built functions. Earth Saari methods. I don't know if that's what you meant when you said function but it's going to open up or create the method. Open. Debit credit and close. So we'll go ahead and run that and you can see that once again so it created a route for everything and then it built out templates for the views it created a directory for credit cards and then for different view pages a helper method and it also included her assets for us. Now we go over here credit cards. You can also see that it built out those. So this is where you could if you were trying to build out any specific custom scopes or whatever you could do. Open accounts equals on credit cards or Soulforce or I guess depending on how you built that out you could call something like open like you would a horse have to create this as a custom scope or an item of some sort. But that's where you can kind of build that out and then in this one. Any credit card that was open would now be available on that page. But that's that's kind of a different deal or a different topic and so I don't want to get too far away from generators so we'll just keep going on. But does anyone have questions to this point. [00:18:51][97.1]
[00:18:54] I don't have a question. I know it's kind of cool that you can run a generator but it doesn't have to be empty. [00:19:03][9.1]
[00:19:04] You can tell it to do some of the things that you want without doing everything like it's gospel does exactly that. [00:19:13][8.2]
[00:19:14] It's super neat because as I was building as I was kind of plannings what I was going to cover today it's incredible the amount of custom customization that Ruby and or that Rails has allowed for you inside of that. I mean you can see just between the three main generators that we ran so far just this. The vast differences between everything and we allowed us to pass them his arguments in the command line that Ben created all of this content for us that created the page and that reading like that. So yeah it is super neat what you can do with one of those. So we'll go ahead and run one more generator for right now and I'm going to run the model how much you want it. What it says about the model. [00:20:04][50.3]
[00:20:07] Can I ask one more question. Muhly. Yeah. So when you run a controller and you put in your parameters like you did it created those four methods. Is there anything else you compassion other than that for controlling for controller. I don't believe so lets me because I know like for the other generators. Why. You can define great the different. [00:20:40][32.9]
[00:20:41] Like the string or the you know what I mean. So let's see. [00:20:46][5.5]
[00:20:47] So if you look at this so I just ran the rails controller help again so it's saying usage run rails generate controller name and then the action action or options. So runs. [00:21:10][22.7]
[00:21:13] Hey. So it's showing us what is generating just the views. It doesn't look like it's accepting any additional arguments based off of this description. I'm sure if you ran auctions what that would allow you to do kind of right here you could see. So let's say we didn't want it to have a whole array. So one cool thing let's actually just redo everything. So instead of Chinnery orgy you can actually just do a D for the league. So let us just so that'll go through and delete everything. And now or go back. Actually it will change a lot. [00:21:57][44.2]
[00:21:58] OK. [00:21:58][0.0]
[00:22:02] So we'll change this to Ajee and then. So the options are something that we have available so it's going to be like Skip routes or no help or let's run this one and then also let's run no assets just so it doesn't get to. So now as it runs you can see the difference. We no longer have these different assets but it looks like those are the only options are only parameters that Rails is accepting for the controller is just to create those pages and the math. So does that answer your question. Yeah it does it does. So now like if we went back to for example before we go into the model controller or model generator let's go to. Get a resource help. This one is a lot more detailed. [00:23:10][68.1]
[00:23:12] So this one is saying okay you can run rails generate resource the name the field which is going on to indicate like a table column type and then it's going to be indexed or an off field type index by default. So index is really neat. That's when you're going to start building the search function into your application. So that's just what this is saying here. By default it will skip it unless you specify and then a Dennett's letting us look at the options so you can look at the options that are available. [00:23:45][32.2]
[00:23:48] It's flooding. This is actually really quartz showing you the different migrations that are available. So if you're going to add to the table or so forth. And as you read the description it will lay out the different arguments and so forth that you can run within that resource generator that school. [00:24:11][23.2]
[00:24:13] So yeah it's really me and the documentation when you're using this dash ATRA The Help function within Relles is super awesome because if you ever forget oh wait can I pass him table arguments on a controller. All you have to do is run that rails h and it explains everything. [00:24:31][17.8]
[00:24:33] So we'll run rails G model and lets his friend h so you can see what's going to happen. [00:24:40][6.8]
[00:24:48] Okay so this one is very complex. I did not expect it to be that complex but it's showing you the same thing. So this one is going to accept those those Oregon. And then as you build it out they'll tell you how you can name it attribute her hairs or arguments can be passed in a special case specifying or digest will generate a password digest filled string type. So [00:25:22][34.5]
[00:25:23] this is cool I've never actually read through this. [00:25:24][1.6]
[00:25:30] You don't have to think of everything upfront. So again it just makes it really nice parent option is let's say and it might actually explain that. I don't know but let's say we had a we are building out a feature such as we use for devices so we wanted a user and an admin. [00:25:48][18.3]
[00:25:49] The parent will allow us to specify its inheritance by reading that code so it's super cool the type of thing that you can run around with within this how. Instead it. Shows the different data types available references are super neat. This is how we do this. Also in the dev camp portfolio in several different instances for blog topics for log comments and so forth that's just what's associating the models with one and not with one another. So you can see how you run that here so in this one photo would reference the model album itself or so does anyone have questions about kind of what it's saying is available inside of this right now. [00:26:35][46.5]
[00:26:41] I have a question and I can probably stupid just to take it says it says in a few different places that it stubs out a new model or is Stubb's something you know is that just a weird way of saying it creates going in. [00:27:01][19.8]
[00:27:01] Let's see the leaves just like I had that question a while ago and I don't remember saying it's so. [00:27:23][21.7]
[00:27:24] What I'm doing. Let's just look it up. So when I don't know question where I want to be sure because I don't want to give you guys the wrong question the wrong answer. Step back. And. [00:27:35][11.3]
[00:27:36] Let's see if it gives us anything subs or fake methods and return and return a specific answer. [00:27:45][8.6]
[00:27:54] Good question. OK. And also. [00:27:57][3.9]
[00:28:08] Seems like to me what it's stuff is kind of in this instance it's not going to give us the exact answer but it seems like it just saying well this is something very consistent but I don't yeah I don't want to give you the wrong answer but my understanding is yes it would say well this is going to create a new model kind of interest. [00:28:29][21.3]
[00:28:29] OK. So I'm just going to do something like this Ariels G scaffold. [00:28:36][6.5]
[00:28:38] Helping kind of just see what it runs their whole you like reading through all this. [00:28:50][12.5]
[00:28:52] Oh I mean when I discovered when I first got started that you could tell that dash H. [00:28:57][5.2]
[00:28:58] It was. It was a life changer for me because I was really able to be like OK and that's what it's doing. Some of it was still really confusing but it still helps. [00:29:06][8.0]
[00:29:10] So it doesn't look super helpful. It's nice that it's Rayder. You don't have to go look it up in three states. [00:29:17][7.0]
[00:29:19] Oh yeah I mean and even if you were to go to the rails documentation it's going to be good. You almost word for word some of this exact same stuff so it makes it nice again. [00:29:29][10.1]
[00:29:29] You don't have to leaving go somewhere else to generate it. So yeah it's not saying Stud's again so let's just run it and see if we can. [00:29:38][9.1]
[00:29:40] Determine if there is any difference. This is really good question. So let's call this one is call this posts. Hey. So it looks like now all it created is all it created was our migration file and then our model. [00:30:16][36.7]
[00:30:17] So let's go look at model. Once again it's a blank bottle since we didn't pass in any any arguments. [00:30:26][9.4]
[00:30:28] For that's so we would run the rails CPB library. And if I try to come back in here and go to posts what what does everyone think will happen at work. [00:30:52][23.8]
[00:30:54] OK. [00:30:54][0.0]
[00:30:56] So let's restart. You say. [00:31:02][6.6]
[00:31:05] OK so what error does are we getting now. Ms no Rouch correct. So what all the other generators did for us is they created the routes automatically for us. Now we wanted to go in and create the routes and fix this. We could run something like. That. And then we would. What will happen now. We hit refresh he wants to throw some ideas out there. [00:31:39][33.9]
[00:31:47] It's still not going to work. [00:31:48][1.0]
[00:31:50] Correct. [00:31:50][0.0]
[00:31:53] Because you didn't throw your doors you have to have an index method. [00:31:56][3.0]
[00:31:57] Correct yes. So we'll be missing our views again. [00:32:00][2.3]
[00:32:02] Oh so well yeah. Once again I'm jumping ahead of myself so we don't even have a controller for this so we're missed. We have the model. [00:32:10][7.7]
[00:32:12] No control or no use. So now it's really broken so we'd have to go manually build out build out a controller and then abuse. So one instance that you might run rails generation are rails micro. Yeah. A health generator for just a model if you're trying just you know to quickly get something done is when you're going to run something for a relationship. So for example each host has an author. So post has many authors and author belongs to Pallister or so forth because then you're not necessarily right away wanting authors but you're wanting the table to the table in the database to communicate with post authors. So those are the main generators so does anyone have any questions to this point. Okay. Awesome. So let's see what I wanted to kind of. So I knew today's was going to be a little shorter but what I wanted to kind of do is go over some really cool I guess bonus material and do a couple additional things within within the generator so the really neat thing. Let's say let's say that for all you're sorry for all your generators you didn't ever want the assets created. What you can do is you can actually come into is going to minimize all these you can come in to Figg config and an application. [00:34:00][108.5]
[00:34:03] And then I'm going to. So you don't have to send a copy or type it all out. I'm [00:34:09][5.4]
[00:34:09] going to paste it in and this is something that you can just quickly get with with a quick google search just rails custom generators so that. So I just pasted this in again. I just got it online earlier today. But what this is going to do is this is going to tell us how to iterate through each generator so it's going to its origin is going to go through the active record. It's template engine. It will run through that Jarobi. If there's a text test framework that we're using is explaining how it works and then how it's saying do not run stylesheets do not run JavaScript so. If you ran this now you want to be safe. [00:35:01][51.8]
[00:35:05] So let's call it just a resource generator. [00:35:07][1.9]
[00:35:09] Or I'll call a scaffold. And let's just call this author's first line like. [00:35:19][9.9]
[00:35:21] SRES. [00:35:21][0.0]
[00:35:26] Okay so now what you can see is it started iterating through that generator but then it skipped the additional assets so it didn't print javascript file for S or a stylesheet. Does that make sense. [00:35:43][16.7]
[00:35:43] Everyone kind of what we did there is something you would do if your really had like your SO sheet's already made. [00:35:52][8.5]
[00:35:53] That's one way you can do it. Jordan in his video. But I'll post after this too. For anyone that kind of wants additional overview what. He explains that really will cause what he'll say is you know let's say that for one generator you do want your stylesheet for another one you don't. You can just come in here and quickly say Okay so for this generator we'll run it with this information. Then when we go on to our next generator and we don't want it we just quickly come in here and change it so that way. He's just quickly customizing the features but ultimately it's you know the framework of authors or the generator of authors is going to be just automatically calling the. Application at the SS. So yeah that's exactly right when you're just not wanting more information when you're not wanting all of those files. So it's super cool if you again if you actually go through the rails documentation there's a lot more that you can customize this. And then the other thing and this also this Jordan covers in the video and I didn't realize that until about 5 minutes before starting the class. So he goes over in great detail. But what you can also do is generate. And generate your own custom template. So what I've got here is I've actually opened up a route the rails. Code source code inside a hub and an issue. I've already gone into the directory. But as you come into this ERP you can see and then we'll go into a scaffold. [00:37:40][107.6]
[00:37:42] So again this should look familiar so it's saying go to the template engine. So that's where we're at. And then it has this templates file. So what rails is doing every time you run the scaffold as it's coming into these files and it's creating. So we actually looked at this right here Lightsey. So this is our edit of. Minimized this. Make sure everyone can read it. So when we go interviews. Articles edit you can see that this looks almost exactly the same the only thing it's doing is calling the table name automatically title wise and then iterating over that for the actual view. But this is very similar to what we have here. [00:38:36][54.8]
[00:38:40] So it's it's just a really neat feature that Rails has built that out for you and the scaffolds. But the neat thing is you can customize them so let's go ahead and go to what you'll do is you'll go to lib. And then let me make sure before a while coding I get. [00:39:00][19.6]
[00:39:01] A little forgetful. So let's see. Think what we'll do first. I don't want to tell you the wrong thing and have you guys remember it that way. OK. So yeah. So you'll create a template directory. And from there we'll go ahead and open this will create another file and we'll call this Jarobi. Oh not a file sorry. A folder B. And from there we can go ahead and run. [00:39:55][54.0]
[00:39:59] One more folder called this Skeff. [00:40:03][3.6]
[00:40:09] So now what we can do. You can see that it's a very similar process to this so we have our live file. It's an end. Ruby has got rails as parsing through the rails and a generator but then we have our ear B scaffold. So far so if we come into scaffold and templates again. We can now create custom templates for any of these. So let's say that we just wanted to call like an email or let's do this. Sorry. So we'll take the index and let's say we didn't want tables that we just wanted it all created into like one vertical or so forth. I'm just going to do exactly what Jordan does in his video and I'm just and that's just copy all of this information over. And then also new file will do index. HCN and all that year. I will post this. So now let's go ahead and change a couple of things so let's just call this paragraph and just so you can see I actually have built in a plug in that as I change the opening tag. It also changes the here. So unless you have that added in just make sure that you're changing both the closing and opening ties when you're doing something like this. But then we'll get rid of what's called this table we'll call it a death. And again that change everything down here. Let's see. We don't need any of this. We'll go ahead and take that out and create another div. This looks like something we don't necessarily need. [00:42:06][117.0]
[00:42:07] Oh we'll leave that in right now. Now go through it. [00:42:18][10.8]
[00:42:24] I'll change the table data to just a paragraph and then it doesn't look like it changed my tags here. So we'll go ahead and close though as we close out here OK. [00:42:45][20.9]
[00:42:50] So doing this live I'm not 100 percent sure the template will display exactly how we want but let's go ahead and run just a new scaffold. [00:42:59][8.3]
[00:43:01] Let's call this lesson's. [00:43:09][7.9]
[00:43:13] Object strained. Take. Now we go on our view all roads front our rails. The beam migrate and our rails. [00:43:31][18.2]
[00:43:34] Pass for now and go ahead and see what we did with our template. OK so it doesn't look like it actually changed. [00:43:49][15.3]
[00:43:51] That's crazy something. [00:43:51][0.5]
[00:43:57] OK. OK. Say I did it just because we're only calling that one feel. You can see here. It's now calling. [00:44:05][7.9]
[00:44:06] When I do the inspect element it's now calling a paragraph instead of before when we are en articles. [00:44:15][8.5]
[00:44:19] We've got ourself a table. So that's just a really neat way to customize your application further so if you know you're going to be creating a lot of scaffolds and you want your index your your new page or your view pages to look but very specific way what you can do is you can just create a template in here once and then every time you run that generator then it will use that it will overwrite the default rails template and use yours. So that's just a neat thing that you can do with generate Raider's to. [00:44:50][31.4]
[00:44:51] Additionally customize it to be a little bit more friendly to what you're looking for. [00:44:55][3.7]
[00:44:58] So any other questions today that's pretty cool I'm going to get that video in. [00:45:08][10.3]
[00:45:09] Yeah. So I will make sure that everyone has that inside of there. [00:45:13][4.0]
[00:45:14] I'll post that in everyone's channels just so they can see Jordan's video and then once this video is completed you'll also have this available but I challenge everyone just to kind of dive a little deeper into generator's because it really is me what you can do. Well some people some people or some Ruby developers will say Oh never use generators. It's know so much easier just to do it by hand. There's definitely a lot of benefits to it when you're in a hurry or you're trying to build out custom features. [00:45:43][29.0]
[00:45:46] And you know Sixt brief succession or so forth. These are a great tool to save you additional time of having to hand type everything but thanks everyone for participating and we're still going to keep going with these so we'll plan on having another webinars this this upcoming Thursday at about 5:00 or. But let us know if you have any questions or if anyone has any specific ideas or specific. Things that they'd like topics that they'd like to see covered in the webinars. Definitely let us know we're always open to hearing what people are kind of looking for within within to help them further their progress here and Otaiba. But you guys have a great night and we'll talk to you soon. [00:46:31][45.8]
[00:46:33] Right or something similar. She answered. [00:46:34][1.4]
[2472.0]