Rails 101: Generators

December 28, 2008

Rails Generators

So you’re just about to crack on and dive into that code, but wait, you don’t even need to do that! Rails comes with generators for basic functionality so you can get going as quickly as possible.

Because Rails follows the strict file structure we covered in the previous article, you can have a large amount of files automatically generated for you, because it knows where to put them.

You also can download a video version of this article, however the article is more in-depth.

How?

Now, as I’ve mentioned before, these files aren’t like what is common in Java development where each IDE has it’s own set which can’t be used in another, just plain and simple Ruby classes, modules, and view files.

You can create your own controllers and models etc, but the beauty of generators is you get up and running quickly. The first, and most important thing to note is you can get help on generators at any time by typing script/generate --help

All the generators are called by typing script/generate followed by the generator type and any options.

Scaffolding

To setup your first applications, the easiest thing to do is use the scaffold generator, this not only sets up your model and controller, but a set of view files, a migration, a routes entry and some tests too.

To begin with lets create an application to store some notes. Each note will have a title and some content as attributes, so the command to generate this is:

script/generate scaffold note title:string body:text

Upon running this command you can see it generates a whole bunch of files, but at this moment we don’t need to worry about them, we’ll just migrate the database and boot up our server.

rake db:migrate script/server

We don’t have anything setup for our root page at the moment, so when you navigate to http://localhost:3000/ you will get the default Rails page. But if you go to the notes path at http://localhost:3000/notes you will see that a basic notes section with all the CRUD functionality is available.

Others

You don’t always need to create all these files, sometimes you might just want a model or a controller, in which case you can simply use the script/generate command again, but you can pass model, controller or migration instead depending on what you want.

One thing to remember with generating controllers is that you don’t actually specify the model name like in scaffold or model generations, you actually specify the controller name, which is plural, so script/generate scaffold note becomes script/generate controller notes

Edit > Undo

Finally if you decide you did something you didn’t want to, you can remove any changes by running the same generator but with script/destroy instead of script/generate

About The Author

-

Ryan Townsend has been developing websites and web applications professionally for almost 5 years and has just graduated with a BSc in Internet Computing from The University of Manchester.

Discussion

7 people have voiced their opinions, voice yours?

-

Mr. Jar said:

Hi Ryan, very clear, I waiting next episodes.

thanks.

Edit | Destroy

-

Tomas Eriksson said:

Just want to say that your screencast are great! Im looking forward future ones.

Edit | Destroy

-

Ryan Townsend said:

Thanks to you both for your comments, I must apologise for the lack of updates recently, I have two major projects on the go, however they are going to be finished and released soon, so I should have more time over the next couple of months to push out more updates – there are plenty more Rails 101 episodes in the works.

I suggest you subscribe to the RSS feed so you’ll get the updates though as and when they are made.

Edit | Destroy

-

Jeffrey Horen said:

Hi Ryan,
Thanks for the sceencasts. They are great!

Edit | Destroy

-

Tim said:

Great screencast, I’ve learned a lot seeing and imitating your moves but does is stop a number 3?

Edit | Destroy

-

Ryan Townsend said:

Thanks Tim,

Yes there is only 3 at the moment, I am currently on holiday, but over the next few weeks expect to see many more. I have quite a few in the works, just waiting to get home and upload them next week.

Edit | Destroy

-

Thomas said:

Hey, like others already have noted: the screencasts are great and easy to understand. Thanks.

Btw. the design of this website is lovely aswell.

Edit | Destroy