我需要有不同版本的 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。
请参阅此了解更多详细信息:在 Windows 上使用 bcrypt 3.0.1 和 ruby2.0 时出现问题 https://stackoverflow.com/questions/18541062/issues-using-bcrypt-3-0-1-with-ruby2-0-on-windows
我基本上做了第一个答案中提到的事情
EDIT
###################################################################
正如下面的答案所指出的,我确实应该在生产和开发中使用相同的版本(即使我只是在学习教程)。
我最终做的是猴子修补 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__)