为什么'捆绑安装'在Rails生成器中失败?

时间:2011-09-27 19:06:07

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

我正在尝试为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中,但是当从生成器运行'bundle install'命令时它失败了。当我从控制台运行“捆绑安装”时,它会安装所有宝石而不会出现任何错误。

为什么会这样?


运行'rails g somegem:install'后我的Gemfile(我从列表中删除了评论):

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"

1 个答案:

答案 0 :(得分:3)

如指出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