我在练习 Ruby on Rails Tutorial 第 6 章,控制台手动创建新用户的时候,报这个错误,求高手指点一下:
NoMethodError: undefined method cost' for BCrypt::Engine:Class
from D:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activemodel-4.
0.2/lib/active_model/secure_password.rb:104:in
password='
from D:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4
.0.2/lib/active_record/attribute_assignment.rb:42:in public_send'
from D:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4
.0.2/lib/active_record/attribute_assignment.rb:42:in
_assign_attribute'
PS: 我用的 bcrypt-ruby 是 3.1.2 版本,不是教程里的 3.0.1 版本,因为在 Gem 里使用 3.0.1 版本时,会报冲突:
You don't have bcrypt-ruby installed in your application. Please add it to your Gemfile and run bundle install Gem::LoadError: can't activate bcrypt-ruby (~> 3.1.2), already activated bcrypt- ruby-3.0.1-x86-mingw32. Make sure all dependencies are added to Gemfile. from D:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/bundler-1.3.5/ lib/bundler/rubygems_integration.rb:224:in `block in replace_gem'
可能是我装的 railsinstaller 自带的 bcrypt-ruby 版本时 3.1.2....
已解决: 1 把 secure_password.rb 里的 gem 'bcrypt-ruby', '~> 3.0.0' 改成 gem 'bcrypt-ruby', '~> 3.1.0'1 2 项目文件夹里 Gemfile 里用 gem 'bcrypt-ruby', '3.1.2'。