Table of Contents

List of Generators

Documentation taken from Netbeans 6.5.1 generator dialog box.

controller

Description

Stubs out a new controller and its views. Pass the controller name, either CamelCased or under_scored, and a list of views as arguments.

To create a controller within a module, specify the controller name as a path like parent_module/controller_name.

This generates a controller class in app/controllers, view templates in app/views/controller_name, a helper class in app/helpers, a functional test suite in test/functional and a helper test suite in test/unit/helpers.

Example
  ./script/generate controller CreditCard open debit credit close

Credit card controller with URLs like /credit_card/debit.

  Controller:      app/controllers/credit_card_controller.rb
  Functional Test: test/functional/credit_card_controller_test.rb
  Views:           app/views/credit_card/debit.html.erb [...]
  Helper:          app/helpers/credit_card_helper.rb
  Helper Test:     test/unit/helpers/credit_card_helper_test.rb
Modules Example
  ./script/generate controller 'admin/credit_card' suspend late_fee

Credit card admin controller with URLs /admin/credit_card/suspend.

  Controller:      app/controllers/admin/credit_card_controller.rb
  Functional Test: test/functional/admin/credit_card_controller_test.rb
  Views:           app/views/admin/credit_card/debit.html.erb [...]
  Helper:          app/helpers/admin/credit_card_helper.rb
  Helper Test:     test/unit/helpers/admin/credit_card_helper_test.rb

integration_test

Description

Stubs out a new integration test. Pass the name of the test, either CamelCased or under_scored, as an argument. The new test class is generated in test/integration/testname_test.rb

Example
  ./script/generate integration_test GeneralStories

creates a GeneralStories integration test in test/integration/general_stories_test.rb

model

Description

Stubs out a new model. Pass the model name, either CamelCased or under_scored, and an optional list of attribute pairs as arguments.

Attribute pairs are column_name:sql_type arguments specifying the model's attributes. Timestamps are added by default, so you don't have to specify them by hand as created_at:datetime updated_at:datetime.

You don't have to think up every attribute up front, but it helps to sketch out a few so you can start working with the model immediately.

This generates a model class in app/models, a unit test in test/unit, a test fixture in test/fixtures/singular_name.yml, and a migration in db/migrate.

Examples:

  ./script/generate model account

creates an Account model, test, fixture, and migration:

  Model:      app/models/account.rb
  Test:       test/unit/account_test.rb
  Fixtures:   test/fixtures/accounts.yml
  Migration:  db/migrate/XXX_add_accounts.rb
  ./script/generate model post title:string body:text published:boolean

creates a Post model with a string title, text body, and published flag.

migration

Description

Stubs out a new database migration. Pass the migration name, either CamelCased or under_scored, and an optional list of attribute pairs as arguments.

A migration class is generated in db/migrate prefixed by a timestamp of the current date and time.

You can name your migration in either of these formats to generate add/remove column lines from supplied attributes: AddColumnsToTable or RemoveColumnsFromTable

Example
  ./script/generate migration AddSslFlag

If the current date is May 14, 2008 and the current time 09:09:12, this creates the AddSslFlag migration db/migrate/20080514090912_add_ssl_flag.rb

  ./script/generate migration AddTitleBodyToPost title:string body:text published:boolean`

This will create the AddTitleBodyToPost in db/migrate/20080514090912_add_title_body_to_post.rb with this in the Up migration:

add_column :posts, :title, :string  
add_column :posts, :body, :text  
add_column :posts, :published, :boolean

And this in the Down migration:

remove_column :posts, :published  
remove_column :posts, :body  
remove_column :posts, :title

mailer

Description

Stubs out a new mailer and its views. Pass the mailer name, either CamelCased or under_scored, and an optional list of emails as arguments.

This generates a mailer class in app/models, view templates in app/views/mailer_name, a unit test in test/unit, and fixtures in test/fixtures.

Example
  ./script/generate mailer Notifications signup forgot_password invoice

creates a Notifications mailer class, views, test, and fixtures:

  Mailer:     app/models/notifications.rb
  Views:      app/views/notifications/signup.erb [...]
  Test:       test/unit/test/unit/notifications_test.rb
  Fixtures:   test/fixtures/notifications/signup [...]

plugin

Description

Stubs out a new plugin. Pass the plugin name, either CamelCased or under_scored, as an argument. Pass –with-generator to add an example generator also.

This creates a plugin in vendor/plugins including an init.rb and README as well as standard lib, task, and test directories.

Example
   ./script/generate plugin BrowserFilters

creates a standard browser_filters plugin:

  vendor/plugins/browser_filters/README
  vendor/plugins/browser_filters/init.rb
  vendor/plugins/browser_filters/install.rb
  vendor/plugins/browser_filters/lib/browser_filters.rb
  vendor/plugins/browser_filters/test/browser_filters_test.rb
  vendor/plugins/browser_filters/tasks/browser_filters_tasks.rake
./script/generate plugin BrowserFilters --with-generator

creates a browser_filters generator also:

  vendor/plugins/browser_filters/generators/browser_filters/browser_filters_generator.rb
  vendor/plugins/browser_filters/generators/browser_filters/USAGE
  vendor/plugins/browser_filters/generators/browser_filters/templates/

scaffold

Description

Scaffolds an entire resource, from model and migration to controller and views, along with a full test suite. The resource is ready to use as a starting point for your RESTful, resource-oriented application.

Pass the name of the model (in singular form), either CamelCased or under_scored, as the first argument, and an optional list of attribute pairs.

Attribute pairs are column_name:sql_type arguments specifying the model's attributes. Timestamps are added by default, so you don't have to specify them by hand as created_at:datetime updated_at:datetime.

You don't have to think up every attribute up front, but it helps to sketch out a few so you can start working with the resource immediately.

For example, scaffold post title:string body:text published:boolean gives you a model with those three attributes, a controller that handles the create/show/update/destroy, forms to create and edit your posts, and an index that lists them all, as well as a map.resources :posts declaration in config/routes.rb.

If you want to remove all the generated files, run script/destroy scaffold ModelName.

Examples:

./script/generate scaffold post
./script/generate scaffold post title:string body:text published:boolean
./script/generate scaffold purchase order_id:integer amount:decimal

session_migration

Description

Creates a migration to add the sessions table used by the Active Record session store. Pass the migration name, either CamelCased or under_scored, as an argument.

Example
  ./script/generate session_migration CreateSessionTable

With 4 existing migrations, this creates the AddSessionTable migration in db/migrate/005_add_session_table.rb

metal

Description

Cast some metal!

Examples:

  ./script/generate metal poller

This will create: Metal: app/metal/poller.rb

observer

Description

Stubs out a new observer. Pass the observer name, either CamelCased or under_scored, as an argument.

The generator creates an observer class in app/models and a unit test in test/unit.

Example
  ./script/generate observer Account

creates an Account observer and unit test:

  Observer:   app/models/account_observer.rb
  Test:       test/unit/account_observer_test.rb

resource

Description

Stubs out a new resource including an empty model and controller suitable for a restful, resource-oriented application. Pass the singular model name, either CamelCased or under_scored, as the first argument, and an optional list of attribute pairs.

Attribute pairs are column_name:sql_type arguments specifying the model's attributes. Timestamps are added by default, so you don't have to specify them by hand as created_at:datetime updated_at:datetime.

You don't have to think up every attribute up front, but it helps to sketch out a few so you can start working with the resource immediately.

This creates a model, controller, helper, tests and fixtures for all of them, and the corresponding map.resources declaration in config/routes.rb

Unlike the scaffold generator, the resource generator does not create views or add any methods to the generated controller.

Examples:

  ./script/generate resource post # no attributes
  ./script/generate resource post title:string body:text published:boolean
  ./script/generate resource purchase order_id:integer amount:decimal

helper

Description

Stubs out a new helper. Pass the helper name, either CamelCased or under_scored.

To create a helper within a module, specify the helper name as a path like parent_module/helper_name.

This generates a helper class in app/helpers and a helper test suite in test/unit/helpers.

Example
  ./script/generate helper CreditCard

Credit card helper.

Helper:     app/helpers/credit_card_helper.rb
Test:       test/unit/helpers/credit_card_helper_test.rb

Modules

Example
  ./script/generate helper 'admin/credit_card'

Credit card admin helper.

  Helper:     app/helpers/admin/credit_card_helper.rb
  Test:       test/unit/helpers/admin/credit_card_helper_test.rb

performance_test

Description

Stubs out a new performance test. Pass the name of the test, either CamelCased or under_scored, as an argument. The new test class is generated in test/performance/testname_test.rb

Example
  ./script/generate performance_test GeneralStories

creates a GeneralStories performance test in test/performance/general_stories_test.rb