什么是 Rails' 脚手架功能

What is Rails’ scaffold functionality

我最近开始在 rails 上尝试 ruby,但我不确定 rails 中的脚手架是什么。

说我 运行 这个命令: rails 生成脚手架想法 name:string description:text picture:string

除了在您的项目目录中创建新文件之外,它到底做了什么?是否为数据库创建 table?

非常感谢。

查看 scaffolding 上的 Rails 指南。

当您使用 generate scaffold 命令时,您正在创建一个迁移(当您 运行 rake db:migrate 时将创建一个 table),视图,css 、脚本文件、初始测试等。将术语 "scaffold" 的字面意思想象成 "a structure around which something is constructed" 并且脚手架开始变得很有意义。

直接从命令行:

[root@mayureshmayur test_scaffold]# rails g scaffold
Running via Spring preloader in process 2715
Usage:
  rails generate scaffold NAME [field[:type][:index] field[:type][:index]] [options]

Options:
      [--skip-namespace], [--no-skip-namespace]             # Skip namespace (affects only isolated applications)
      [--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
  -y, [--stylesheets], [--no-stylesheets]                   # Generate Stylesheets
                                                            # Default: true
  -se, [--stylesheet-engine=STYLESHEET_ENGINE]              # Engine for Stylesheets
                                                            # Default: scss
      [--assets], [--no-assets]                             # Indicates when to generate assets
                                                            # Default: true
  -ss, [--scaffold-stylesheet], [--no-scaffold-stylesheet]  # Indicates when to generate scaffold stylesheet
                                                            # Default: true
  -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
  -t, [--test-framework=NAME]                # Test framework to be invoked
                                             # Default: test_unit

TestUnit options:
      [--fixture], [--no-fixture]    # Indicates when to generate fixture
                                     # Default: true
  -r, [--fixture-replacement=NAME]   # Fixture replacement to be invoked
      [--system-tests=SYSTEM_TESTS]  # Skip system test files
                                     # Default: test_unit

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

Asset options:
  -j, [--javascripts], [--no-javascripts]       # Generate JavaScripts
                                                # Default: true
  -je, [--javascript-engine=JAVASCRIPT_ENGINE]  # Engine for JavaScripts
                                                # Default: coffee

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

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.

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

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