Ruby on Rails/Built-In Rails Tools/Generators

Previous: Getting Started/Running the Server Index Next: Built-In_Rails_Tools/Make a generator

Generators edit

Introduction edit

Rails comes with a number of generators which are used to create stub files for models, controllers, views, unit tests, migrations and more. Generators are accessed through the command-line script RAILS_ROOT/script/generate

All commands look like

 rails generate generator generator-options

To see what options are available, just enter

 rails generate

and Rails will show you all available options and what generators are currently installed. By default, you can choose from the different generators. The most important are:

  • controller
  • helper
  • mailer
  • migration
  • model
  • scaffold

If you want more information on different generators, simply enter the generator commend e.g. "script/generate model" in the console and you will get information to this specific command and examples explaining what the generator does.

You can use the generator multiple times for the same controller, but be careful: it will give you the option to overwrite your controller file (to add the actions you specify). As long as you have not modified the controller this might be fine, but if you have already added code then make sure you do not overwrite it and go back and manually add the action methods.

Generate a Model edit

To generate a model use the following:

 rails generate model ModelName column:datatype column:datatype [...]

Replace ModelName with the CamelCase version of your model name. For example:

 rails generate model Person name:string age:integer

This would generate the following:

exists  app/models/
exists  test/unit/
exists  test/fixtures/
create  app/models/person.rb
create  test/unit/person_test.rb
create  test/fixtures/people.yml
exists  db/migrate
create  db/migrate/20090607101912_create_people.rb

Any necessary directories will be automatically created. Existing ones will not be replaced. The file app/models/person.rb contains the Person class. test/unit/person_test.rb contains the unit test stub for the Person class. test/fixtures/people.yml contains a stub for test data which will be used to populate the test database during test runs. The db/migrate/20090607101912_create_people.rb contains the database migration stub for the Person class. Note that the timestamp at the beginning of the file (20090607101912) will always be different depending on the time you created the file. Also note how Rails pluralizes the class name to use the plural form as the corresponding table, fixture and migration names.

Generate a Controller edit

To generate a controller use the following:

 rails generate controller ControllerName [actions]

Replace ControllerName with the CamelCase version of your controller name. When no actions are given, Rails will create a Controller that responds to all 7 REST actions (new, create, update, edit, destroy, index & show)

 rails generate controller People

would generate the following output:

 exists  app/controllers/
 exists  app/helpers/
 create  app/views/people
 exists  test/functional/
 exists  test/unit/helpers/
 create  app/controllers/people_controller.rb
 create  test/functional/people_controller_test.rb
 create  app/helpers/people_helper.rb
 create  test/unit/helpers/people_helper_test.rb

The file app/controllers/people_controller.rb contains the PeopleController class. test/functional/people_controller_test.rb contains the functional test stub for the PersonController class. app/helpers/people_helper.rb is a stub for helper methods which will be made available to that controller and its associated views. Inside app/views/people you will find the created templates for the controller. Depending on the given parameters, there will be different files.


Generate a Migration edit

To generate a migration use the following:

 rails generate migration MigrationName column:datatype column:datatype [...]

Replace MigrationName with the CamelCase version of your migration name. For example:

 generate migration AddCityToPerson

This would generate the following:

exists  db/migrate
create  db/migrate/20090607103358_add_city_to_person.rb

Migration are automatically generated each time you construct a new model, so you do not need to generate migrations by hand for each model. Typically you will use the migration generator when you need to change an existing model or need join tables. Again, the timestamp starting the filename will be different.