• home
  • forum
  • my
  • kt
  • download
  • Rails 101: Generators

    Author: 2008-08-29 09:26:34 From:

    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 screencast, you can have a large amount of files automatically generated for you, because it knows where to put them.

    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.

    In my application I am going to store notes, so I'll type script/generate scaffold and the name of my model which is "note", followed by it's attributes, which are a name and field type separated by a comma (e.g. name:string). Scaffolding from Rails 2.0 onwards works out the name of the controller by pluralizing the model name, pre-Rails 2.0 you had to specify the controller name separately as another parameter.

    For the attributes I'll have a title which will be a string, a body which will be some text and maybe how many times it's been viewed using an integer:

    title:string body:text hits:integer

    You can see from the terminal that 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, but if we go to our new controller, mine is "notes", so the URL is "/notes". You can see that we've got a list generated for us, and we can already go in and add, edit and remove notes, yet we haven't written one single line of code yet!

    Now you don't always want 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, integration_test, 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.

    There are many more generators however, we will concentrate on them in further screencasts, scaffolding is the important one to get you one your way with development. You can look the others up using the help command I mentioned earlier if you wish.

    Oops, I did something wrong!

    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.

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Database Related (2)
      Getting Started (10)
      Helpers (5)
      Image Manipulation (2)
      Security (4)

    New

    Hot