Ruby-on-rails 更改 rvm 使用的 rails 版本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18290080/
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
Change rails version used by rvm
提问by Pol0nium
Here are my local gems :
这是我当地的宝石:
$ gem list
*** LOCAL GEMS ***
actionmailer (4.0.0, 3.2.14)
actionpack (4.0.0, 3.2.14)
activemodel (4.0.0, 3.2.14)
activerecord (4.0.0, 3.2.14)
activerecord-deprecated_finders (1.0.3)
activeresource (3.2.14)
activesupport (4.0.0, 3.2.14)
arel (4.0.0, 3.0.2)
atomic (1.1.13)
builder (3.1.4, 3.0.4)
bundler (1.3.5)
bundler-unload (1.0.1)
erubis (2.7.0)
hike (1.2.3)
i18n (0.6.5)
journey (1.0.4)
json (1.8.0)
mail (2.5.4)
mime-types (1.24)
minitest (4.7.5)
multi_json (1.7.9)
polyglot (0.3.3)
rack (1.5.2, 1.4.5)
rack-cache (1.2)
rack-ssl (1.3.3)
rack-test (0.6.2)
rails (3.2.14)
railties (4.0.0, 3.2.14)
rake (10.1.0)
rdoc (3.12.2)
rubygems-bundler (1.2.2)
rubygems-update (2.0.7)
rvm (1.11.3.8)
sprockets (2.10.0, 2.2.2)
sprockets-rails (2.0.0)
thor (0.18.1)
thread_safe (0.1.2)
tilt (1.4.1)
treetop (1.4.15)
tzinfo (0.3.37)
But when I type rails -vI get this :
但是当我输入时,rails -v我得到了这个:
$ rails -v
Rails 4.0.0
$ which rails
/Users/polonium/.rvm/rubies/ruby-2.0.0-p247/bin/rails
How can I specify rvm to use rails version 3.2.14 ?
如何指定 rvm 以使用 rails 版本 3.2.14 ?
Thanks in advance
提前致谢
回答by Santhosh
You can create a new rails app with a particular rails version, like this
您可以使用特定的 rails 版本创建一个新的 rails 应用程序,如下所示
rails _3.2.14_ new myApp
回答by 7stud
How can I specify rvm to use rails version 3.2.14 ?
如何指定 rvm 以使用 rails 版本 3.2.14 ?
rvm has nothing to do with rails. rvm is used to manage multiple ruby installations. And each of your ruby installations can be associated with multiple gemsets. For instance, say you have ruby 1.9.3 installed and you created two gemsets for ruby 1.9.3: gemsetA and gemsetB. If you tell rvm to use ruby 1.9.3 with gemsetA, that means:
rvm 与 rails 无关。rvm 用于管理多个 ruby 安装。并且您的每个 ruby 安装都可以与多个 gemset 相关联。例如,假设您安装了 ruby 1.9.3,并且为 ruby 1.9.3 创建了两个 gemset:gemsetA 和 gemsetB。如果您告诉 rvm 将 ruby 1.9.3 与 gemsetA 一起使用,则意味着:
Your ruby programs will be executed by ruby 1.9.3.
Your ruby programs can require any gem in gemsetA (which then allows your program to use the methods (or classes) defined in gemsetA), but any gems in gemsetB cannot be seen by your ruby program.
您的 ruby 程序将由 ruby 1.9.3 执行。
您的 ruby 程序可能需要 gemsetA 中的任何 gem(然后允许您的程序使用 gemsetA 中定义的方法(或类)),但是您的 ruby 程序无法看到 gemsetB 中的任何 gem。
Here's a concrete example:
这是一个具体的例子:
~$ rvm list
rvm rubies
ruby-1.8.7-p370 [ i686 ]
* ruby-1.9.3-p194 [ x86_64 ]
=> ruby-2.0.0-p0 [ x86_64 ]
ruby-2.0.0-p247 [ x86_64 ]
# => - current
# =* - current && default
# * - default
~$ rvm use 1.9.3-p194
Using /Users/7stud/.rvm/gems/ruby-1.9.3-p194
.
.
~$ rvm gemset list (This lists only the gemsets for the current ruby version)
gemsets for ruby-1.9.3-p194 (found in /Users/7stud/.rvm/gems/ruby-1.9.3-p194)
=> (default)
global
programming
rails3tutorial
rails4
~$ rvm gemset use programming
Using ruby-1.9.3-p194 with gemset programming
After I do that, my ruby programs will be executed by ruby 1.9.3 and any gems in the programming gemset can be required into my ruby program. You can use a shortcut to perform both those commands in one step:
在我这样做之后,我的 ruby 程序将由 ruby 1.9.3 执行,并且编程 gemset 中的任何 gem 都可以在我的 ruby 程序中使用。您可以使用快捷方式在一个步骤中执行这两个命令:
rvm use ruby 1.9.3-p194@programming
You just combine the ruby version and the gemset with an '@' between them.
您只需将 ruby 版本和 gemset 与它们之间的“@”组合起来。
But when I type rails -v I get this :
$ rails -v Rails 4.0.0
但是当我输入 rails -v 我得到这个:
$ rails -v Rails 4.0.0
That's because the current gemset contains the gem for rails 4.0.0. If you want to see $ rails -voutput Rails 3.2.14, then you need to tell rvm to switch to a gemset that contains the rails 3.2.14 gem.
这是因为当前的 gemset 包含 Rails 4.0.0 的 gem。如果您想查看$ rails -voutput Rails 3.2.14,那么您需要告诉 rvm 切换到包含 rails 3.2.14 gem 的 gemset。
However, you can make rvm automatically switch to the proper rails version and gemset for your rails project. In your Gemfile, add a comment after the ruby version:
但是,您可以让 rvm 自动切换到适合您的 rails 项目的 rails 版本和 gemset。在您的 Gemfile 中,在 ruby 版本后添加注释:
ruby '2.0.0'
#ruby-gemset=railstutorial4_gems
Then whenever you switch to the directory containing your rails project, rvm will automatically switch the current ruby to ruby 2.0.0 and the current gemset to railstutorial4_gems. If you change directories out of your rails app, rvm will change the current ruby and the current gemset back to what they were.
然后每当您切换到包含 Rails 项目的目录时,rvm 会自动将当前的 ruby 切换到 ruby 2.0.0,将当前的 gemset 切换到 railstutorial4_gems。如果您从 Rails 应用程序中更改目录,rvm 会将当前的 ruby 和当前的 gemset 更改回原来的状态。
I'm just a rails beginner, but here are the steps I use to create a new project, which are straight out of the railstutorial book (http://ruby.railstutorial.org/ruby-on-rails-tutorial-book)
我只是一个 rails 初学者,但这里是我用来创建一个新项目的步骤,这些步骤直接来自 railstutorial 书 ( http://ruby.railstutorial.org/ruby-on-rails-tutorial-book)
1)
1)
.../rails_projects$ rvm use <ruby version here>@<new gemset name here> --create
e.g.
.../rails_projects$ rvm use ruby-1.9.3-p194@myapp_gemset --create
2)
2)
.../rails_projects$ gem install rails --version 3.2.14
Because the current gemset is the myapp gemset, that command installs the rails 3.2.14 gem into the myapp gemset.
因为当前的 gemset 是 myapp gemset,所以该命令会将 rails 3.2.14 gem 安装到 myapp gemset 中。
3)
3)
.../rails_projects$ rails new myapp
.../rails_projects$ cd myapp
The current gemset is still myapp_gemset.
当前的 gemset 仍然是 myapp_gemset。
4)
4)
.../rails_projects/myapp$ rails -v
Rails 3.2.14
In case anyone was wondering what the heck the following two gemsets are all about:
如果有人想知道以下两个宝石是什么鬼东西:
gemsets for ruby-1.9.3-p194 (found in /Users/7stud/.rvm/gems/ruby-1.9.3-p194)
=> (default)
global
rvm creates those two gemsets for every ruby version you install. After you install a ruby version, if you don't create a gemset yourself for that ruby version, and you install a gem, then the gem goes into the (default) gemset. And, if you want all your gemsets to contain a certain gem, you can switch to the global gemset and install the gem there.
rvm 为您安装的每个 ruby 版本创建这两个 gemset。安装 ruby 版本后,如果您没有为该 ruby 版本自己创建 gemset,并且安装了 gem,那么 gem 将进入(默认)gemset。而且,如果您希望所有 gemset 都包含某个 gem,您可以切换到全局 gemset 并在那里安装 gem。
Update:-------
更新:-------
To maintain compatibility with other ruby version managers, you can specify the ruby version and gemset name for your project in a different file rather than in the Gemfile:
为了保持与其他 ruby 版本管理器的兼容性,您可以在不同的文件中而不是在 Gemfile 中为您的项目指定 ruby 版本和 gemset 名称:
$ cd ~/rails_projects/myapp
~/rails_projects/myapp$ echo 2.0.0 > .ruby-version
~/rails_projects/myapp$ echo myapp_gemset > .ruby-gemset
You'll still get the same automatic ruby version and gemset switching when you cdinto your project's directory. See the rvm docs here.
当您cd进入项目目录时,您仍将获得相同的自动 ruby 版本和 gemset 切换。请在此处查看 rvm 文档。
回答by mpapis
the path you found /Users/polonium/.rvm/rubies/ruby-2.0.0-p247/bin/railsis not proper path you would found in standard rvm installation while doing proper use of rvm, what you would see should be: /Users/polonium/.rvm/gems/ruby-2.0.0-p247/bin/rails
您找到的路径/Users/polonium/.rvm/rubies/ruby-2.0.0-p247/bin/rails不是您在正确使用 rvm 时在标准 rvm 安装中会找到的正确路径,您将看到的应该是:/Users/polonium/.rvm/gems/ruby-2.0.0-p247/bin/rails
to get it working properly try this flow:
要使其正常工作,请尝试以下流程:
rvm use 2.0.0
rvm gemset empty
rvm use @rails3 --create
gem install rails -v "~>3.2"
rvm use @rails4 --create
gem install rails -v "~>4"
this way now you can switch between this two rails installations with:
通过这种方式,您现在可以在这两个导轨安装之间切换:
rvm use 2.0.0@rails3
rails -v # rails 3.2...
rvm use 2.0.0@rails4
rails -v # rails 4.0...
回答by KKB
you should do: gem install 'rails' -v '3.2.13'
你应该这样做: gem install 'rails' -v '3.2.13'
You should use a clean gemset, if you are using rvm, you can try:
您应该使用干净的 gemset,如果您使用的是 rvm,您可以尝试:
rvm gemset create rvm gemset use
rvm gemset 创建 rvm gemset 使用
Now you have a clean gemset, it's time to install rails,
现在你有了一个干净的 gemset,是时候安装 rails,
gem install rails -v '3.2.13'
gem install rails -v '3.2.13'
and then create a rails app,
然后创建一个 rails 应用程序,
rails new app_name
rails 新 app_name
回答by KKB
"railties (4.0.0," is the problem you have. I guess it was installed by mistake?
“railties (4.0.0,”是你遇到的问题。我猜是安装错了?
Uninstall that version using, gem uninstall railties, and then picking the option with 4.0.0.
使用 gem 卸载 railties 卸载该版本,然后选择 4.0.0 的选项。
Now the command "rails -v" will return your expected version which is "3.2.14"
现在命令“rails -v”将返回您预期的版本“3.2.14”
回答by jrichocean
Make sure to check the global gemset by first making sure you are in the ruby version you intend to use
首先确保您使用的是您打算使用的 ruby 版本,从而确保检查全局 gemset
$ruby -v
Then switch to the global gemset (the global gemset applys to all of the gemsets so be sure to keep it clean and universal...or global, hence the name)
然后切换到全局 gemset(全局 gemset 适用于所有 gemset 所以一定要保持它干净和通用......或全局,因此得名)
$rvm gemset use global
After that check the gem list for gems that can affect it like the "railties" gem that Harsha mentioned by typing
之后,检查宝石列表中可能影响它的宝石,例如 Harsha 通过键入提到的“railties”宝石
$gem list
Sometimes the global gemset can be populated by an accidental gem that will affect all other gemsets. If you find a misplaced gem you can uninstall it simply with
有时,全局 gemset 可能会被意外的 gem 填充,这会影响所有其他 gemset。如果您发现放错位置的 gem,您可以简单地将其卸载
$gem uninstall examplegem
or for a specific version of that gem pass in the -v with the version in quotes
或者对于该 gem 的特定版本,在 -v 中使用引号中的版本
$gem uninstall examplegem -v"3.2.1"
And read 7stud's comment on creating and using RVM for changing between different gemsets.
并阅读 7stud 关于创建和使用 RVM 在不同 gemset 之间进行更改的评论。
回答by Gamaliel
You could add the version of Rails you want, with:
您可以添加所需的 Rails 版本:
$ gem install rails --version 3.2.14
And then create an app with that version:
然后使用该版本创建一个应用程序:
$ rails _3.2.14_ new myapp
Finally, you can check it out the content of the "Gemfile" to corroborate:
最后,您可以查看“Gemfile”的内容来证实:
...
gem 'rails', '3.2.14'
...

