← 返回命令列表

Linux command

rails-generate 命令

文件

复制后可按需替换文件名、目录或参数。

常用示例

Generate model

rails generate model [Name] [field:type]

Generate controller

rails generate controller [Name] [action]

Generate scaffold

rails generate scaffold [Name] [field:type]

Generate migration

rails generate migration [Name]

说明

rails generate creates boilerplate files from templates for common Rails application components. It scaffolds models with database migrations, controllers with associated views and routes, and full CRUD interfaces through the scaffold generator, following Rails naming conventions and directory structure automatically. Each generator produces the appropriate set of files including source code, test stubs, and configuration entries. The --pretend flag previews what would be created without writing files, and --skip-routes prevents automatic route additions. Custom generators can be created to match project-specific patterns, and the inverse operation rails destroy removes all files a generator created.

参数

model _name_
Generate model.
controller _name_
Generate controller.
scaffold _name_
Generate full CRUD.
migration _name_
Generate migration.
-p, --pretend
Preview without creating.
--skip-routes
Don't add routes.

FAQ

What is the rails-generate command used for?

rails generate creates boilerplate files from templates for common Rails application components. It scaffolds models with database migrations, controllers with associated views and routes, and full CRUD interfaces through the scaffold generator, following Rails naming conventions and directory structure automatically. Each generator produces the appropriate set of files including source code, test stubs, and configuration entries. The --pretend flag previews what would be created without writing files, and --skip-routes prevents automatic route additions. Custom generators can be created to match project-specific patterns, and the inverse operation rails destroy removes all files a generator created.

How do I run a basic rails-generate example?

Run `rails generate model [Name] [field:type]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does model _name_ do in rails-generate?

Generate model.