Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Rails) : NoMethodError: undefined method cost' for BCrypt::Engine:Class

When I learn "Ruby on Rails Tutorial", and I want to create a User on console:

irb(main):001:0> User.create(name:"gsky",email:"[email protected]",
irb(main):002:1* password:"aaaaaa",password_confirmation:"aaaaaa")

then, I getting the following error message:

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:inpassword='
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'

This is user model:

class User < ActiveRecord::Base

  before_save { self.email = email.downcase }

  validates :name,  presence: true, length: { maximum: 50 }

  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

  validates :email, presence: true,
                    format: { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }

   has_secure_password

   validates :password, length: { minimum: 6 }

end
like image 580
gsky Avatar asked Feb 18 '14 12:02

gsky


2 Answers

Add bcrypt-ruby to your Gemfile as specified below:

     gem 'bcrypt-ruby', '3.1.2'

then run bundle update from your project root directory and bundle install

like image 131
osleonard Avatar answered Oct 19 '22 03:10

osleonard


When i saw "Ruby On Rails Tutorial" I have met the same problem, I solved it by set Gemfile from:

gem 'bcrypt-ruby', '3.0.1' 

to:

gem 'bcrypt-ruby', '3.1.2' 

then run:

bundle install
like image 8
qingxp9 Avatar answered Oct 19 '22 04:10

qingxp9