Ruby-on-rails 如何在 rbenv 中使用多个 Rails 版本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8877772/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:39:14  来源:igfitidea点击:

How do you use multiple rails versions with rbenv?

ruby-on-railsrbenv

提问by aciniglio

Is it possible to use multiple versions of rails using rbenv (e.g. 2.3 and 3.1)? This was easy with gemsets in rvm, but I'm wondering what the best way is to do it now that I've switched to rbenv (also, I'm looking for a way to do it without rbenv-gemset).

是否可以使用 rbenv(例如 2.3 和 3.1)使用多个版本的 rails?这对于 rvm 中的 gemsets 很容易,但是我想知道现在我已经切换到 rbenv 的最佳方法是什么(另外,我正在寻找一种没有 rbenv-gemset 的方法)。

回答by Nathan

not sure if you got an answer to this, but I thought I'd offer what I did and it seemed to work.

不确定你是否得到了答案,但我想我会提供我所做的并且它似乎有效。

So once you get rbenv installed, and you use it to install a specific ruby version, you can install multiple versions of rails to for that ruby.

因此,一旦您安装了 rbenv,并使用它来安装特定的 ruby​​ 版本,您就可以为该 ruby​​ 安装多个版本的 rails。

STEP 1. Install whatever version(s) of rails you want per ruby version

步骤 1. 为每个 ruby​​ 版本安装您想要的任何版本的 rails

% RBENV_VERSION=1.9.2-p290 rbenv exec gem install rails --version 3.0.11

By using the "RBENV_VERSION=1.9.2-p290" prefix in your command line, you're specifying which ruby rbenv should be concerned with.

通过在命令行中使用“RBENV_VERSION=1.9.2-p290”前缀,您可以指定应该关注哪个 ruby​​ rbenv。

Then following that with the "rbenv exec" command, you can install rails. Just use the version flag as in the example to specify which version you want. Not sure if you can install multiple versions in one shot, but I just run this command as many times as needed to install each version I want.

然后使用“rbenv exec”命令,您可以安装rails。只需使用示例中的版本标志来指定您想要的版本。不确定您是否可以一次性安装多个版本,但我只是根据需要多次运行此命令来安装我想要的每个版本。

Note: This will all be managed within your rbenv directory, so it's perfectly safe and contained.

注意:这一切都将在您的 rbenv 目录中进行管理,因此它非常安全且包含在内。

STEP 2. Build a new rails project by specifying the rails version you want.

步骤 2. 通过指定所需的 rails 版本构建一个新的 rails 项目。

% RBENV_VERSION=1.9.2-p290 rbenv exec rails _3.0.11_ new my_project

STEP 3. Don't forget to go into that project and set the local rbenv ruby version.

STEP 3. 不要忘记进入该项目并设置本地 rbenv ruby​​ 版本。

% cd my_project
% rbenv local 1.9.2-p290


Now if you want to delete this project, just delete it as normal.

现在,如果您要删除此项目,只需照常删除即可。

If you want to delete / manage a rails version from rbenv gems, you can use regular gem commands, just prefix your command line with:

如果您想从 rbenv gems 中删除/管理 rails 版本,您可以使用常规 gem 命令,只需在命令行前加上:

% RBENV_VERSION=1.9.2-p290 rbenv exec gem {some command}

And of course, you can delete a complete ruby version and all its shims, etc that are managed within rbenv pretty easily. I like how self contained everything is.

当然,您可以非常轻松地删除在 rbenv 中管理的完整 ruby​​ 版本及其所有垫片等。我喜欢一切都是独立的。

Hope this helps.

希望这可以帮助。

For reference, this is a pretty good walk through of at least some of this stuff:

作为参考,这是对至少其中一些内容的一个很好的介绍:

http://ascarter.net/2011/09/25/modern-ruby-development.html

http://ascarter.net/2011/09/25/modern-ruby-development.html

回答by Michael Trojanek

There is a rbenv plugin called rbenv-gemsetwhich should behave similar to the rvm gemset-command but since rbenv was never intended to work this way, I haven't tried it.

有一个名为rbenv-gemset的 rbenv 插件,它的行为应该与rvm gemset-command类似,但由于 rbenv 从未打算以这种方式工作,我还没有尝试过。

I usually manage Rails versions with Bundler as Nathan suggested in the comments of one of the other answers. I create a Gemfile with my desired Rails version, run bundle install, create the Rails application, let it replace the Gemfile and let Bundler take over:

我通常使用 Bundler 管理 Rails 版本,正如 Nathan 在其他答案之一的评论中所建议的那样。我用我想要的 Rails 版本创建了一个 Gemfile,运行bundle install,创建 Rails 应用程序,让它替换 Gemfile 并让 Bundler 接管:

mkdir my-rails-app
cd my-rails-app
echo "source 'https://rubygems.org'" > Gemfile
echo "gem 'rails', '3.2.17'" >> Gemfile
bundle install

bundle exec rails new . --force --skip-bundle
bundle update

If you want more detail, I wrote an article on my blogabout it.

如果你想了解更多细节,我在我的博客上了一篇关于它的文章

Hope it helps!

希望能帮助到你!