什么是Rails的脚手架功能

时间:2015-07-26 21:09:46

标签: ruby-on-rails scaffolding

我最近开始尝试在轨道上使用红宝石,我不确定在轨道上是什么脚手架。

说我运行这个命令: rails生成scaffold idea name:string description:text picture:string

除了在项目目录中创建新文件外,它到底做了什么?它是否为数据库创建了一个表?

非常感谢。

2 个答案:

答案 0 :(得分:1)

查看scaffolding上的Rails指南。

当您使用generate scaffold命令时,您将创建一个迁移(在运行rake db:migrate时将创建一个表),视图,css,脚本文件,初始测试等。术语"脚手架的字面含义" as"构建某物的结构"和脚手架开始有很大意义。

答案 1 :(得分:0)

直接从命令行:

[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`