Ruby-on-rails 指定创建新应用程序时使用的 rails 版本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/379141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Specifying rails version to use when creating a new application
提问by hectorsq
I have two versions of rails (2.1.0 and 2.2.2) installed in my computer.
我的计算机中安装了两个版本的 rails(2.1.0 和 2.2.2)。
When I create a new application, is it possible to specify that I want to use the older (2.1.0) version?
当我创建一个新的应用程序时,是否可以指定我要使用旧的 (2.1.0) 版本?
回答by hectorsq
回答by Hardik
Here is the command which I use normally:
这是我通常使用的命令:
rails _version_ new application_name
for example rails _2.1.0_ new my_app
例如 rails _2.1.0_ new my_app
Here is the list of all available rails versions so far:
这是迄今为止所有可用 Rails 版本的列表:
回答by mikej
I was having some trouble using rails _version_ new application_name(the resulting project was still generated for the newest version of Rails installed.)
我在使用时遇到了一些问题rails _version_ new application_name(结果项目仍然是为安装的最新版本的 Rails 生成的。)
After a bit of digging I found an articleby Michael Trojanekwith an alternative approach. This works by creating a folder with a Gemfile specifying the desired version of Rails and then using bundle exec rails...so that Bundler takes care of running the appropriate version of rails. e.g. to make a new Rails 4.2.9 projects the steps are:
经过一番挖掘,我找到了Michael Trojanek的一篇文章,其中包含另一种方法。这是通过创建一个带有 Gemfile 的文件夹来指定所需的 Rails 版本,然后使用,bundle exec rails...以便 Bundler 负责运行适当版本的rails. 例如,制作一个新的 Rails 4.2.9 项目的步骤是:
mkdir myapp
cd myapp
echo "source 'https://rubygems.org'" > Gemfile
echo "gem 'rails', '4.2.9'" >> Gemfile
bundle install
bundle exec rails new . --force --skip-bundle
bundle update
回答by Rajkaran Mishra
As rightly pointed out by @mikej for Rails 5.0.0 or above, you should be following these steps:
正如@mikej 对于Rails 5.0.0 或更高版本正确指出的那样,您应该遵循以下步骤:
Create a directory for your application along with a Gemfile to specify your desired Rails version and let bundler install the dependent gems:
为您的应用程序创建一个目录以及一个 Gemfile 来指定您想要的 Rails 版本并让 bundler 安装依赖的 gems:
$ mkdir myapp
$ cd myapp
$ echo "source 'https://rubygems.org'" > Gemfile
$ echo "gem 'rails', '5.0.0.1'" >> Gemfile
$ bundle install
Check that the correct version of rails has been installed: $ bundle exec rails -v
检查是否安装了正确版本的 rails: $ bundle exec rails -v
Now create your application, let Rails create a new Gemfile (or rather overwrite the existing one by using the --forceflag) and instead of installing the bundle (--skip-bundle) update it manually:
现在创建您的应用程序,让 Rails 创建一个新的 Gemfile(或者使用--force标志覆盖现有的),而不是安装包 ( --skip-bundle) 手动更新它:
$ bundle exec rails new . --force --skip-bundle
If you check the entry for rails in Gemfile, it should be like this:
如果你检查 rails in 的条目Gemfile,它应该是这样的:
gem 'rails', '~> 5.0.0', '>= 5.0.0.1'
You should update it to the exact version needed for the application:
您应该将其更新为应用程序所需的确切版本:
gem 'rails', '5.0.0.1'
Now, the final step:
现在,最后一步:
$ bundle update
回答by Keltia
You can generate the skeleton with either version and require the one you want in config/environment.rb:
您可以使用任一版本生成骨架,并需要您想要的版本config/environment.rb:
# Specifies gem version of Rails to use when vendor/rails is not present
RAILS_GEM_VERSION = '2.1.2' unless defined? RAILS_GEM_VERSION
or use the "rails" command form the version you want anyway.
或者使用您想要的版本的“rails”命令。
回答by Thiago Arrais
You should also take a look at "freezing" your Rails gems into the app. This helps a lot with deployment, specially in shared hosting environments.
您还应该看看“冻结”您的 Rails gems 到应用程序中。这对部署有很大帮助,特别是在共享托管环境中。
Just change the RAILS_GEM_VERSIONvariable in config/environment.rband issue the freeze rake task:
只需更改RAILS_GEM_VERSION变量config/environment.rb并发出冻结耙任务:
rake rails:freeze:gems
回答by Prakash
There are two ways to achieve this:
有两种方法可以实现这一点:
one as suggested in accepted answer:
一个在接受的答案中建议:
gem install rails -v 2.1.0 #only when the gem has not been installed in the desired ruby version you are using, so that you don't get error on next step
rails _2.1.0_ new my_app
and alternative method is to create gemfile with desired rails version before initializing rails project
替代方法是在初始化 rails 项目之前创建具有所需 rails 版本的 gemfile
mkdir my_app
cd my_app
echo "source 'https://rubygems.org'" > Gemfile
echo "gem 'rails', '2.1.0'" >> Gemfile
bundle install
bundle exec rails new . --force --skip-bundle
I have written about this in details in my article
我在我的文章中详细写过这个

