使用has_secure_password时出错。无法加载此类文件 - 2.0 / bcrypt_ext

时间:2013-09-08 15:45:28

标签: ruby-on-rails bcrypt-ruby bundle-install

我是Ruby on Rails的新手,所以我正在使用Rails 4进行教程敏捷Web开发。但是在用户身份验证上我遇到了一个我无法解决的错误... 我已经创建了一个表单,因此用户可以登录,因为我在user.rb文件中使用has_secure_password:

class User < ActiveRecord::Base
    has_secure_password
  validates :name, presence: true, uniqueness: true
end

但是当我运行服务器时,我得到错误:

cannot load such file -- 2.0/bcrypt_ext

继承应用程序跟踪

app/models/user.rb:2:in `<class:User>'
app/models/user.rb:1:in `<top (required)>'
app/controllers/users_controller.rb:7:in `index'
This error occurred while loading the following files:
   bcrypt
   bcrypt_ext
   2.0/bcrypt_ext

我有包括宝石'bcrypt-ruby','〜&gt;在gemfile中3.0.0',但它仍然不起作用。

我在类似的问题中读到它与“Bundle install”有关,但我无法解决问题。

提前致谢

3 个答案:

答案 0 :(得分:4)

当然,

gem install bcrypt-ruby 

这将使has_secure_password有效。还记得在看到更改后重新启动rails服务器。

答案 1 :(得分:0)

我们只能想象你在使用什么平台,但我假设是* nix派生词。 编辑Gemfile是不够的 - bundle install命令将为您完成工作。 您还需要安装本机编译工具链,因为bcrypt_ruby正在安装期间构建一些本机扩展。

答案 2 :(得分:0)

如果你正在使用 RVM ,那么你应该为global gemset安装bcrypt gem:

rvm gemset use global
gem install bcrypt
rvm gemset use whatever_gemset_you_use_previously

(由于后者的弃用消息,我使用bcrypt而不是bcrypt-ruby