Neu: Das englische Ruby on Rails 4.0 Buch.

5.2. Generieren eines Scaffolds

Legen wir zuerst mit Scaffolding eine Liste von Bundeskanzlern an. Dazu müssen Sie erst mal eine neue Rails-Applikation anlegen:
MacBook:~ xyz$ rails new history 
[...]
MacBook:~ xyz$ cd history 
MacBook:history xyz$
Schauen wir uns die Optionen von Scaffolding an:
MacBook:history xyz$ rails generate scaffold
Usage:
  rails generate scaffold NAME [field[:type][:index] field[:type][:index]] [options]

Options:
      [--skip-namespace]                        # Skip namespace (affects only isolated applications)
      [--old-style-hash]                        # Force using old style hash (:foo => 'bar') on Ruby >= 1.9
  -o, --orm=NAME                                # Orm to be invoked
                                                # Default: active_record
      [--force-plural]                          # Forces the use of a plural ModelName
  -y, [--stylesheets]                           # Generate Stylesheets
                                                # Default: true
  -se, [--stylesheet-engine=STYLESHEET_ENGINE]  # Engine for Stylesheets
                                                # Default: scss
  -c, --scaffold-controller=NAME                # Scaffold controller to be invoked
                                                # Default: scaffold_controller
      [--assets]                                # Indicates when to generate assets
                                                # Default: true

ActiveRecord options:
      [--migration]            # Indicates when to generate migration
                               # Default: true
      [--timestamps]           # Indicates when to generate timestamps
                               # Default: true
      [--parent=PARENT]        # The parent class for the generated model
      [--indexes]              # Add indexes for references and belongs_to columns
                               # Default: true
  -t, [--test-framework=NAME]  # Test framework to be invoked
                               # Default: test_unit

TestUnit options:
      [--fixture]                   # Indicates when to generate fixture
                                    # Default: true
  -r, [--fixture-replacement=NAME]  # Fixture replacement to be invoked

ScaffoldController options:
  -e, [--template-engine=NAME]  # Template engine to be invoked
                                # Default: erb
      [--helper]                # Indicates when to generate helper
                                # Default: true

Runtime options:
  -f, [--force]    # Overwrite files that already exist
  -p, [--pretend]  # Run but do not make any changes
  -q, [--quiet]    # Supress status output
  -s, [--skip]     # Skip files that already exist

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.

    Attributes are field arguments specifying the model's attributes. You can
    optionally pass the type and an index to each field. For instance:
    "title body:text tracking_id:integer:uniq" will generate a title field of
    string type, a body with text type and a tracking_id as an integer with an
    unique index. "index" could also be given instead of "uniq" if one desires
    a non unique index.

    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 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 resources :posts declaration
    in config/routes.rb.

    If you want to remove all the generated files, run
    'rails destroy scaffold ModelName'.

Examples:
    `rails generate scaffold post`
    `rails generate scaffold post title body:text published:boolean`
    `rails generate scaffold purchase amount:decimal tracking_id:integer:uniq`
MacBook:history xyz$
Ich mache es kurz: Für unseren jetzigen Kenntnisstand können wir rails generate scaffold wie rails generate model benutzen (siehe Abschnitt 4.2, „Datenbank/Model anlegen“). Legen wir jetzt das Scaffold für Bundeskanzler an:
MacBook:history xyz$ rails generate scaffold chancellor first_name last_name birthday:date day_of_death:date inauguration:date
      invoke  active_record
      create    db/migrate/20120508171951_create_chancellors.rb
      create    app/models/chancellor.rb
      invoke    test_unit
      create      test/unit/chancellor_test.rb
      create      test/fixtures/chancellors.yml
       route  resources :chancellors
      invoke  scaffold_controller
      create    app/controllers/chancellors_controller.rb
      invoke    erb
      create      app/views/chancellors
      create      app/views/chancellors/index.html.erb
      create      app/views/chancellors/edit.html.erb
      create      app/views/chancellors/show.html.erb
      create      app/views/chancellors/new.html.erb
      create      app/views/chancellors/_form.html.erb
      invoke    test_unit
      create      test/functional/chancellors_controller_test.rb
      invoke    helper
      create      app/helpers/chancellors_helper.rb
      invoke      test_unit
      create        test/unit/helpers/chancellors_helper_test.rb
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/chancellors.js.coffee
      invoke    scss
      create      app/assets/stylesheets/chancellors.css.scss
      invoke  scss
      create    app/assets/stylesheets/scaffolds.css.scss
MacBook:history xyz$

Autor

Stefan Wintermeyer