Linux command
rails 命令
文件
复制后可按需替换文件名、目录或参数。
常用示例
Create a new Rails application
rails new [app_name]
Create a new app with PostgreSQL
rails new [app_name] --database=postgresql
Start the development server
rails server
Open an interactive console
rails console
Generate a model
rails generate model [ModelName] [field:type] [field:type]
Generate a controller
rails generate controller [ControllerName] [action1] [action2]
Run database migrations
rails db:migrate
List all routes
rails routes
说明
rails is the command-line interface for the Ruby on Rails web application framework. It provides tools for creating, developing, and managing Rails applications through a unified interface. The command operates in two contexts: outside an application (primarily rails new for creating projects) and inside an application directory (all other commands). Within a project, commands are typically invoked via bin/rails to ensure the correct bundled version is used. Rails emphasizes convention over configuration, and its CLI reflects this by providing generators that create boilerplate code following Rails conventions. The framework integrates database management, testing, asset compilation, and server operation into cohesive command-line workflows.
参数
- new _app_name_
- Create a new Rails application with standard directory structure
- server, s
- Start the Puma web server (default port 3000)
- console, c
- Open an interactive IRB session with the application context
- generate, g
- Run code generators for models, controllers, migrations, etc.
- destroy, d
- Remove files created by a generator
- db:migrate
- Run pending database migrations
- db:create
- Create the database
- db:seed
- Load seed data from db/seeds.rb
- db:setup
- Create database, load schema, and seed data
- routes
- Display all defined routes
- test
- Run the test suite
- --help, -h
- Display help for any command
- --version
- Show the Rails version
- -e, --environment _ENV_
- Specify environment (development, test, production)
- -p, --port _PORT_
- Set server port (default 3000)
FAQ
What is the rails command used for?
rails is the command-line interface for the Ruby on Rails web application framework. It provides tools for creating, developing, and managing Rails applications through a unified interface. The command operates in two contexts: outside an application (primarily rails new for creating projects) and inside an application directory (all other commands). Within a project, commands are typically invoked via bin/rails to ensure the correct bundled version is used. Rails emphasizes convention over configuration, and its CLI reflects this by providing generators that create boilerplate code following Rails conventions. The framework integrates database management, testing, asset compilation, and server operation into cohesive command-line workflows.
How do I run a basic rails example?
Run `rails new [app_name]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does new _app_name_ do in rails?
Create a new Rails application with standard directory structure