【问题标题】:How do I specify different versions of a gem for development and production如何为开发和生产指定不同版本的 gem
【发布时间】:2014-01-03 23:52:28
【问题描述】:

我需要有不同版本的 gem 用于开发和生产,所以我将以下内容放在我的 gemfile 中。

group :development, :test do
  gem 'rspec-rails', '2.11.0'
  gem 'bcrypt-ruby', '3.1.2'
end

group :production do
  gem 'rails_12factor'
  gem 'bcrypt-ruby', '3.0.1'  
end

但如果我尝试做bundle install 甚至只是rails console 我会收到上述错误

我试过了

bundle install --without production

但我仍然收到错误消息。 供参考: 我需要这样做,因为我正在阅读 rails 教程,并且在 windows、ruby 2.0.0 和 bcrypt 和 Heroku 之间出现了冲突 所以我在 Windows 上使用 bcrypt 3.1.2(修改了活动记录 gemfile) 和 Heroku 上的 bcrypt 3.0.1。

更多详情请看这里: Issues using bcrypt 3.0.1 with ruby2.0 on Windows

我基本上做了第一个答案中提到的事情

编辑

###################################################################

正如下面的答案所指出的,我真的应该在生产和开发中使用相同的版本(即使我只是在学习教程)。 我最终做的是猴子修补 ActiveModel 以使用

gem 'bcrypt-ruby', '3.1.2'

而不是

gem 'bcrypt-ruby', '~> 3.0.0'

在安全密码中。

我通过将以下内容放入 lib/secure_password_using_3_1_2.rb 来完成此操作

module ActiveModel
  module SecurePassword
    extend ActiveSupport::Concern

    module ClassMethods

      def has_secure_password
        # Load bcrypt-ruby only when has_secure_password is used.
        # This is to avoid ActiveModel (and by extension the entire framework) being dependent on a binary library.
        #gem 'bcrypt-ruby', '~> 3.0.0'
        gem 'bcrypt-ruby', '3.1.2'
        require 'bcrypt'

        attr_reader :password

        validates_confirmation_of :password
        validates_presence_of     :password_digest

        include InstanceMethodsOnActivation

        if respond_to?(:attributes_protected_by_default)
          def self.attributes_protected_by_default
            super + ['password_digest']
          end
        end
      end
    end
  end
end

然后将以下内容添加到 config/environment.rb

require File.expand_path('../../lib/secure_password_using_3_1_2.rb', __FILE__)

【问题讨论】:

  • 你说你得到“上述错误”,但你没有提供错误。
  • 对不起...它在我的标题中,但它太长了。基本上它抱怨我在 gem 文件中有两个版本。我现在不在电脑前,所以我现在不能给出确切的信息

标签: ruby-on-rails ruby ruby-on-rails-3 heroku


【解决方案1】:

这个怎么样?

gem "my_gem", ENV["RAILS_ENV"] == "production" ? "2.0" : "1.0"

RAILS_ENV=production bundle

【讨论】:

  • 我认为这行得通。我会将其标记为答案,但我认为“正确”的解决方案是我在问题的编辑版本中所做的猴子补丁
【解决方案2】:

简短的回答是你不能轻易做到这一点。 Bundler 旨在强制所有 gem 在开发和生产之间使用相同的版本。使用不同的版本可能会导致细微的错误。

为什么不想在生产环境中运行 3.1.2?

【讨论】:

  • 我在heroku上使用rails 3.2.16,它在has_secure_password.rb中要求bcrypt ~》3.0.0。我在我的 Windows 安装中破解了该文件。我以为我无法为生产做到这一点,或者我可以提供一个替代的 has_secure_password.rb 吗?
【解决方案3】:

我知道我迟到了,但我在任何地方都找不到答案。

我试图找到这个问题的答案,因为我想将预发布的 gem 部署到我的暂存环境,并将完整的 gem 版本部署到我的生产环境。在发布之前,我不希望我的生产环境使用“1.0.2.pre1”之类的东西或类似的东西,从而拥有“1.0.2”版本。原因说来话长:)

version = "3.0.1"

group :development, :test do
    version = "~> 3.1.2"
end

gem 'bcrypt-ruby', version

如果您有分配变量的开发/测试组,它只会运行该块。

【讨论】:

  • 这在生产时不起作用,因为当您运行bundle install 并提交新的Gemfile.lock 时,3.1.2 版本将被写入其中并随后用于生产。跨度>
猜你喜欢
  • 2016-10-18
  • 2021-06-23
  • 2013-05-15
  • 2019-11-10
  • 1970-01-01
  • 1970-01-01
  • 2018-03-31
  • 2018-03-08
  • 1970-01-01
相关资源
最近更新 更多