Generators

In Rails, generators are simply scripts that use templates to create boilerplate code and improve your workflow saving you a quite a bit of time.

rails generate model ModellName

rails generate controller ListController show edit

rails generate scaffold ModelName ControllerName

rails generate migration AddNewTable

rails generate plugin PluginName

rails generate integration_test TestName

rails generate session_migration

Usage: rails generate GENERATOR [args] [options]

General options:

-h, [--help] # Print generator's options and usage

-p, [--pretend] # Do a test run and show you what files will be generated without actually generating them.

-f, [--force] # Overwrite files that already exist

-s, [--skip] # Skip files that already exist

-q, [--quiet] # Suppress status output

Types of Generators

controller

application_record

benchmark

channel

generator

helper

integration_test

jbuilder

job

mailbox

mailer

migration

model

resource

scaffold

scaffold_controller

system_test

task

active_record:application_record

active_record:multi_db

erb:controller

erb:mailer

erb:scaffold

stimulus

test_unit:channel

test_unit:generator

test_unit:install

test_unit:mailbox

test_unit:plugin

Generate a Model

rails g model ModelName

rails generate scaffold

A scaffold in Rails is a full set of model, database migration for that model, controller to manipulate it, views to view and manipulate the data, and a test suite for each of the above.

The following command will generate a scaffold for a single resource called Task:

rails generate scaffold Task title:string

Creating and Customising Rails Generators & Templates

Since Rails 3.0, generators are built on top of Thor. Thor provides powerful options parsing and a great API for manipulating files.

Last updated