如何创建一个新的Rails应用程序
让我们看看创建一个新的Rails应用程序的默认方法。
它将创建一个包含21个宝石的默认新应用。安装过程将持续几分钟。除其他事项外,它将创建文件、目录,并运行第一个基于webpack的编译。
前提条件:必须安装ruby、bundler、rails、node和yarn。
只需运行:
$> rails new myapp
然后去喝杯咖啡休息一下 ☕
所有可用选项的列表
要列出所有可用的选项,只需运行rails new --help。在撰写本文时(Rails 6.1.3),它将输出 。
$> rails new --help
Usage:
rails new APP_PATH [options]
Options:
[--skip-namespace], [--no-skip-namespace] # Skip namespace (affects only isolated engines)
[--skip-collision-check], [--no-skip-collision-check] # Skip collision check
-r, [--ruby=PATH] # Path to the Ruby binary of your choice
# Default: /Users/david/.rbenv/versions/3.0.0/bin/ruby
-m, [--template=TEMPLATE] # Path to some application template (can be a filesystem path or URL)
-d, [--database=DATABASE] # Preconfigure for selected database (options: mysql/postgresql/sqlite3/oracle/sqlserver/jdbcmysql/jdbcsqlite3/jdbcpostgresql/jdbc)
# Default: sqlite3
[--skip-gemfile], [--no-skip-gemfile] # Don't create a Gemfile
-G, [--skip-git], [--no-skip-git] # Skip .gitignore file
[--skip-keeps], [--no-skip-keeps] # Skip source control .keep files
-M, [--skip-action-mailer], [--no-skip-action-mailer] # Skip Action Mailer files
[--skip-action-mailbox], [--no-skip-action-mailbox] # Skip Action Mailbox gem
[--skip-action-text], [--no-skip-action-text] # Skip Action Text gem
-O, [--skip-active-record], [--no-skip-active-record] # Skip Active Record files
[--skip-active-job], [--no-skip-active-job] # Skip Active Job
[--skip-active-storage], [--no-skip-active-storage] # Skip Active Storage files
-P, [--skip-puma], [--no-skip-puma] # Skip Puma related files
-C, [--skip-action-cable], [--no-skip-action-cable] # Skip Action Cable files
-S, [--skip-sprockets], [--no-skip-sprockets] # Skip Sprockets files
[--skip-spring], [--no-skip-spring] # Don't install Spring application preloader
[--skip-listen], [--no-skip-listen] # Don't generate configuration that depends on the listen gem
-J, [--skip-javascript], [--no-skip-javascript] # Skip JavaScript files
[--skip-turbolinks], [--no-skip-turbolinks] # Skip turbolinks gem
[--skip-jbuilder], [--no-skip-jbuilder] # Skip jbuilder gem
-T, [--skip-test], [--no-skip-test] # Skip test files
[--skip-system-test], [--no-skip-system-test] # Skip system test files
[--skip-bootsnap], [--no-skip-bootsnap] # Skip bootsnap gem
[--dev], [--no-dev] # Set up the application with Gemfile pointing to your Rails checkout
[--edge], [--no-edge] # Set up the application with Gemfile pointing to Rails repository
[--master], [--no-master] # Set up the application with Gemfile pointing to Rails repository main branch
[--rc=RC] # Path to file containing extra configuration options for rails command
[--no-rc], [--no-no-rc] # Skip loading of extra configuration options from .railsrc file
[--api], [--no-api] # Preconfigure smaller stack for API only apps
[--minimal], [--no-minimal] # Preconfigure a minimal rails app
-B, [--skip-bundle], [--no-skip-bundle] # Don't run bundle install
--webpacker, [--webpack=WEBPACK] # Preconfigure Webpack with a particular framework (options: react, vue, angular, elm, stimulus)
[--skip-webpack-install], [--no-skip-webpack-install] # Don't run Webpack install
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
Rails options:
-h, [--help], [--no-help] # Show this help message and quit
-v, [--version], [--no-version] # Show Rails version number and quit
Description:
The 'rails new' command creates a new Rails application with a default
directory structure and configuration at the path you specify.
You can specify extra command-line arguments to be used every time
'rails new' runs in the .railsrc configuration file in your home directory,
or in $XDG_CONFIG_HOME/rails/railsrc if XDG_CONFIG_HOME is set.
Note that the arguments specified in the .railsrc file don't affect the
defaults values shown above in this help message.
Example:
rails new ~/Code/Ruby/weblog
This generates a skeletal Rails installation in ~/Code/Ruby/weblog.
跳过一个或多个功能
如果你想跳过一个或多个功能,例如,如果你不想要turbolinks或系统测试,请运行
$> rails new myapp --skip-turbolink --skip-system-test
如果你想跳过的选项太多,下面的命令将根据选项创建一个应用程序。
$> rails new myapp --rc=options
这里的 "options "是一个文件,包含你需要的任何标志。
跳过(几乎)所有的功能
现在有一种新的方法(从Rails 6.1开始)来创建一个简约的Rails应用。它可以在几秒钟内建立一个新的应用程序,只需要7个宝石(在撰写本文时)。
$> rails new myapp --minimal
下面是创建的Gemfile。
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '3.0.0'
gem 'rails', '~> 6.1.3'
gem 'sqlite3', '~> 1.4'
gem 'puma', '~> 5.0'
gem 'sass-rails', '>= 6'
group :development, :test do
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end
group :development do
gem 'listen', '~> 3.3'
end
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
以下是不包括的内容的列表
- action_cable:将websockets集成到Rails中。
- action_mailbox:将电子邮件收件箱行为整合到Rails控制器中。
- action_mailer:用Rails发送电子邮件。
- action_text:增加将HTML放入Rails的能力。
- active_job : 增加了创建后台作业的功能。
- active_storage : 能够通过第三方工具如AWS上传文件。
- bootsnap:更快地启动Rails应用程序
- jbuilder:建立JSON响应
- spring : 更快地启动Rails应用(与bootnap类似,但不同的是......)
- system_tests : 功能测试能力
- turbolinks:现在已经废弃,被Turbo取代。一旦应用程序在浏览器中加载,允许SPA模式的导航。
- webpack : 著名的JavaScript捆绑器。
为什么你应该依赖新鲜的、新的、简约的Rails应用程序?
使用标志--minimal有以下好处。
-
你可以深入了解你的堆栈的默认宝石。花点时间了解已经包含的几个宝石,然后你就会(非常有可能)花时间了解你添加的那个宝石。相反,如果你在创建一个新的Rails应用程序时没有使用"--minimal "标志,那么风险就是最终会出现一些害怕不理解的情况,再加上一些死的代码,以及恼人的bug--不是由你的代码引起的,而是由于相互冲突的宝石造成的摩擦。
-
这样一来,隔离日常生产应用中出现的问题就非常容易了。只需在你的软件仓库外重新创建另一个极简的应用程序,并将所有的文件放在里面(也许是通过复制/粘贴)来重新创建你正在处理的错误。
-
花点时间看看你是否需要上面列出的任何功能。不是所有的应用程序都需要有 "电子邮件收件箱 "的功能。许多开发者跳过了 "春天 "这个选项。也许你会做一些功能测试,这要感谢Cypress。实际上,你的Rails应用看起来像默认的可能性几乎为零。