← Home

Rails Generate scaffold

Reference & Command Builder

Published: 2023-05-11
Author: Harrison Broadbent
Type: Reference & Command Builder

Rails Generate Scaffold

Rails Generate Scaffold (rails g scaffold) is the generator for an entire REST-ful resource. The Scaffold Controller generator will create everything you need for an entire REST-ful resource, including -

This generator creates all the boilerplate for a new REST-ful resource. It will generate a controller with the 7 standard REST-ful actions, plus corresponding views and forms.

Command Builder for rails g scaffold

rails g scaffold 

Model Name

Model fields (as pairs)

Generator Options

Runtime Options

# Command Options
❯ rails g scaffold
  rails generate scaffold NAME [field[:type][:index] field[:type][:index]] [options]

      [--skip-namespace], [--no-skip-namespace]              # Skip namespace (affects only isolated engines)
      [--skip-collision-check], [--no-skip-collision-check]  # Skip collision check
      [--force-plural], [--no-force-plural]                  # Forces the use of the given model name
  -o, --orm=NAME                                             # ORM to be invoked
                                                            # Default: active_record
      [--model-name=MODEL_NAME]                              # ModelName to be used
      [--resource-route], [--no-resource-route]              # Indicates when to generate resource route
                                                            # Default: true
      [--api], [--no-api]                                    # Indicates when to generate api
  -c, --scaffold-controller=NAME                             # Scaffold controller to be invoked
                                                            # Default: scaffold_controller

ActiveRecord options:
        [--migration], [--no-migration]        # Indicates when to generate migration
                                              # Default: true
        [--timestamps], [--no-timestamps]      # Indicates when to generate timestamps
                                              # Default: true
        [--parent=PARENT]                      # The parent class for the generated model
        [--indexes], [--no-indexes]            # Add indexes for references and belongs_to columns
                                              # Default: true
        [--primary-key-type=PRIMARY_KEY_TYPE]  # The type for primary key
  --db, [--database=DATABASE]                  # The database for your model's migration. By default, the current environment's primary database is used.
  -t,   [--test-framework=NAME]                # Test framework to be invoked
                                              # Default: rspec

Rspec options:
  [--fixture], [--no-fixture]                    # Indicates when to generate fixture
  [--fixture-replacement=NAME]                   # Fixture replacement to be invoked
  [--singleton], [--no-singleton]                # Supply to create a singleton controller
  [--controller-specs], [--no-controller-specs]  # Generate controller specs
  [--request-specs], [--no-request-specs]        # Generate request specs
                                                # Default: true
  [--view-specs], [--no-view-specs]              # Generate view specs
                                                # Default: true
  [--helper-specs], [--no-helper-specs]          # Generate helper specs
                                                # Default: true
  [--routing-specs], [--no-routing-specs]        # Generate routing specs
                                                # Default: true

ScaffoldController options:
      [--helper], [--no-helper]            # Indicates when to generate helper
                                          # Default: true
      [--skip-routes], [--no-skip-routes]  # Don't add routes to config/routes.rb.
  -e, [--template-engine=NAME]             # Template engine to be invoked
                                          # Default: tailwindcss
      [--jbuilder], [--no-jbuilder]        # Indicates when to generate jbuilder
                                          # Default: true

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

# Command 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

    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.

    As a special case, specifying 'password:digest' will generate a
    password_digest field of string type, and configure your generated model,
    controller, views, and test suite for use with Active Model
    has_secure_password (assuming they are using Rails defaults).

    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
    'bin/rails destroy scaffold ModelName'.

    `bin/rails generate scaffold post`
    `bin/rails generate scaffold post title:string body:text published:boolean`
    `bin/rails generate scaffold purchase amount:decimal tracking_id:integer:uniq`
    `bin/rails generate scaffold user email:uniq password:digest`