【问题标题】:Why 'bundle install' fails in Rails generator?为什么 Rails 生成器中的“捆绑安装”失败?
【发布时间】:2011-09-27 19:06:07
【问题描述】:

我正在尝试为 Rails 3.1 创建自定义生成器。我写了这个:

module SomeGem
  module Generators
    class InstallGenerator < Rails::Generators::Base
      source_root File.expand_path('../templates', __FILE__)
      desc "This adds devise"

      def install
        gem "devise"
        run "bundle install"
      end
    end
  end
end

但是当我运行这个生成器(rails g somegem:install,在新生成的 rails 应用程序中)时,我得到了这个错误:

 gemfile  devise
 run      bundle install

Could not find gem 'devise (>= 0)' in any of the gem sources listed in your Gemfile.
Run `bundle install` to install missing gems.

我的生成器将 Devise 正确添加到 Gemfile,但从生成器运行“捆绑安装”命令时失败。当我从控制台运行“捆绑安装”时,它会安装所有 gem,没有任何错误。

为什么会这样?


这是我运行“rails g somegem:install”后的 Gemfile(我从列表中删除了 cmets):

source 'http://rubygems.org'
source 'http://gemcutter.org'
source "http://gems.github.com"

gem 'rails', '3.1.0'
gem 'mysql2'

group :assets do
  gem 'sass-rails', "  ~> 3.1.0"
  gem 'coffee-rails', "~> 3.1.0"
  gem 'uglifier'
end

gem 'jquery-rails'

group :test do
  # Pretty printed test output
  gem 'turn', :require => false
end

gem 'therubyracer'
gem "devise"

【问题讨论】:

  • 确保在运行bundle install 命令时已连接到网络

标签: ruby ruby-on-rails-3.1 generator bundler thor


【解决方案1】:

正如 here 所指出的,这是 Bundler 中的一个错误。 让它发挥作用:

module SomeGem
  module Generators
    class InstallGenerator < Rails::Generators::Base
      source_root File.expand_path('../templates', __FILE__)
      desc "This adds devise"

      def install
        gem "devise"
        Bundler.with_clean_env do
          run "bundle install"
        end
      end
    end
  end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    • 2013-10-25
    • 2015-10-19
    • 1970-01-01
    相关资源
    最近更新 更多