【发布时间】:2010-09-27 14:33:47
【问题描述】:
我的计算机中安装了两个版本的 rails(2.1.0 和 2.2.2)。
当我创建一个新应用程序时,是否可以指定我要使用旧 (2.1.0) 版本?
【问题讨论】:
标签: ruby-on-rails
我的计算机中安装了两个版本的 rails(2.1.0 和 2.2.2)。
当我创建一个新应用程序时,是否可以指定我要使用旧 (2.1.0) 版本?
【问题讨论】:
标签: ruby-on-rails
请注意您在 Rails 中使用的 ruby 版本。
为特定版本的 Rail 创建新项目的命令可能不适合您。我对此有一些问题。问题是我默认的 ruby 版本是 3.0.0。这个版本不适用于 Rails 5。然后我安装了 ruby 2.7.5 并默认切换到它。只有这样我才能为 Rails 5 和 7 制作项目。
如果你想要与 ruby 2.7.5 相同的环境
rvm install ruby-2.7.5
默认切换到这个版本
rvm --default use 2.7.5
安装捆绑器和 webpacker
gem install bundler
gem install webpacker
安装最新的 rails(即 7 个)
gem install rails
测试一下
rails new test_app_6
cd test_app_6
rails s
检查本地主机 3000
http://localhost:3000
然后停止服务器(control + c)并安装 Rails 5
gem install rails -v 5.2.6
测试一下
rails _5.2.6_ new test_app_5
cd test_app_5
rails s
检查本地主机 3000
http://localhost:3000
你准备好了!
【讨论】:
有两种方法可以实现:
接受的答案中建议的一个:
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
另一种方法是在初始化 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
我已经在我的article中详细介绍了这一点
【讨论】:
正如@mikej 针对Rails 5.0.0 或更高版本 正确指出的那样,您应该遵循以下步骤:
为您的应用程序创建一个目录以及一个 Gemfile 以指定您所需的 Rails 版本并让 bundler 安装依赖的 gem:
$ mkdir myapp
$ cd myapp
$ echo "source 'https://rubygems.org'" > Gemfile
$ echo "gem 'rails', '5.0.0.1'" >> Gemfile
$ bundle install
检查是否安装了正确版本的rails:$ bundle exec rails -v
现在创建您的应用程序,让 Rails 创建一个新的 Gemfile(或者使用 --force 标志覆盖现有的),而不是安装包 (--skip-bundle) 手动更新它:
$ bundle exec rails new . --force --skip-bundle
如果您检查Gemfile 中的rails 条目,应该是这样的:
gem 'rails', '~> 5.0.0', '>= 5.0.0.1'
您应该将其更新为应用程序所需的确切版本:
gem 'rails', '5.0.0.1'
现在,最后一步:
$ bundle update
【讨论】:
我在使用 rails _version_ new application_name 时遇到了一些问题(生成的项目仍然是为安装的最新版本的 Rails 生成的。)
经过一番挖掘后,我发现an article 和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
【讨论】:
bundle update,它也会更新rails!!
bundle update 是必需的,因为正在使用手动更新 (--skip-bundle)。 Rails 不会升级,因为 Gemfile 中指定了特定版本(示例中为 4.2.9。)
--force 正在覆盖 Gemfile
bundle exec rails new... 运行了在结束的 Gemfile 中指定的 rails 版本被替换。)
gem 'rails', '5.0.0.1' >> Gemfile 并且在使用--force 选项运行bundle exec rails new 之后,它在Gemfile 中的gem 'rails', '~> 5.0.0', '>= 5.0.0.1'。现在当我运行bundle update 时,它会将rails 更新为5.0.4(在Gemfile.lock 中),但我希望使用rails 版本5.0.0.1
这是我常用的命令:
rails _version_ new application_name
例如rails _2.1.0_ new my_app
以下是目前所有可用的 Rails 版本列表:
【讨论】:
我发现 here 是一个未记录的选项,可以使用旧版本的 Rails 创建新应用程序。
rails _2.1.0_ new myapp
【讨论】:
_3.1.3_ new sample_app /home/ninad/.rbenv/versions/1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems.rb: 314:在bin_path': can't find gem railties (["3.1.3"]) with executable rails (Gem::GemNotFoundException) from /home/ninad/.rbenv/versions/1.9.2-p290/bin/rails:19:in 您还应该看看将 Rails gem“冻结”到应用程序中。这对部署有很大帮助,尤其是在共享托管环境中。
只需更改config/environment.rb 中的RAILS_GEM_VERSION 变量并发出freeze rake 任务:
rake rails:freeze:gems
【讨论】:
您可以使用任一版本生成骨架,并在 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
或者使用你想要的版本的“rails”命令。
【讨论】: