Skip to Main Content
Professional Ruby on Rails™
book

Professional Ruby on Rails™

by Noel Rappin
February 2008
Intermediate to advanced content levelIntermediate to advanced
479 pages
14h
English
Wrox
Content preview from Professional Ruby on Rails™

15.3. Writing a Generator

The Rails generator system is a simple but powerful way to create a family of files from a set of templates based on user commands. The Rails core defines generators for controllers, models, mailers, integration tests, plugins, scaffolds, and a couple of other things. In addition to supporting more complex plugins, generators can stand on their own to automate repetitive tasks. Examples might include custom scaffolding for controllers and models, standard layout templates, or test suites.

15.3.1. Basic Generator Functionality

Generators are called via the script/generate command. The general format is as follows:

$ruby script/generate generator_name arg1 arg2 --opt1 --opt2

Every generator responds to a set of common option flags, which are described in the following table.

FlagMeaning
-c --svnAdd new files to subversion, if available.
-h --helpShow the help message.
-f --forceOverwrite existing files without asking.
-p --pretendShow the list of files that would be created, but don't actually create any files.
-q --quietNo output.
-s --skipSkip existing files without asking.
-t --backtraceShow a stack trace on error.
-v --verboseVerbose output.

The arguments and options are parsed for you in the parent generator class before your generator is initialized. You have the ability to add new option arguments to your generator.

Generators come in two flavors: basic and named. In a basic generator, the arguments are placed in the instance variable args, and command-line ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Ruby on Rails® Bible

Ruby on Rails® Bible

Timothy Fisher
Rails 4 in Action

Rails 4 in Action

Yehuda Katz, Rebecca Skinner, Stephen Klabnik, Ryan Bigg

Publisher Resources

ISBN: 9780470223888Purchase book