由于 Bundler 版本冲突,生成控制器失败

Failed generating controller due to Bundler version conflict

我正在学习 Rails Ruby Rails 上的教程,作者是 Michael Hartlhttps://www.railstutorial.org/book

我使用以下命令生成了一个控制器:

rails generate controller StaticPages home help

这会生成以下有关版本冲突的错误:

check_version_conflict': can't activate bundler-1.12.4, already
activated bundler-1.13.0.pre.1 (Gem::LoadError)

我不知道要使用哪个版本的捆绑器。 bundler 的当前版本是:1.13.pre.1


以下命令继续失败,因为大约有五个 gem 依赖项未能自动安装,其中包括 listennokigiri

bundle install --without production

我尝试手动安装相关的 gem,但我仍然遇到问题。

如何解决生成 Rails 控制器时 Bundlercheck_version_conflict 问题?

我会接受指示删除当前 Ruby 库并从头开始安装新开发环境的答案。

Bundler 将安装项目特定版本的 gem,这样您就不必管理全局依赖项。

实际上,如果您使用捆绑器安装 Rails 并且还使用 sudo gem install rails 或类似的东西安装它,您的计算机上将有两个版本。默认情况下,调用rails将引用全球版本。

如果您调用 bundle exec rails(或 bundle exec <gem_name>),它将调用特定于捆绑程序的版本。

解决 Bundler 问题的十个步骤

  1. (可选)卸载 Ruby。有很多方法可以做到这一点,这里是一个:https://superuser.com/questions/194051/how-to-completely-remove-ruby-ruby-gems-on-mac-os-x-10-6-4
  2. (可选)使用 rbenv 安装 Ruby。按照此处的说明进行操作:https://github.com/rbenv/rbenv
  3. 创建一个存储你未来 Rails app
  4. 的 repo 目录

从命令行:

mkdir repo
cd repo
  1. 安装 Bundler 并为目录创建一个 Gemfile

从命令行:

gem install bundler
bundle init
  1. 用你的编辑器打开 repo/Gemfile,并配置它以指示 Bundler 安装哪个版本的 Rails

repo/Gemfile中:

source "https://rubygems.org"                                

gem "rails", "4.2.6"
  1. 通过 Bundler 安装 Rails

从命令行:

bundle install
  1. 使用 Bundler 创建一个新的 Rails 应用程序,并将 cd 添加到其中

从命令行:

bundle exec rails new whatevs
cd whatevs
  1. 您的 Rails 应用默认会有一个 Gemfile。打开它并添加您希望在您的应用中使用的 gem。

repo/whatevs/Gemfile中:

gem 'nokogiri', '1.6.8'
  1. repo/whatevs/ 目录,通过 Bundler 安装您应用的 Gems

从命令行:

bundle install
  1. repo/whatevs/目录,生成一个控制器

从命令行:

bundle exec rails generate controller static_pages home help