undefined method `key?' for nil:NilClass

空扰寡人 提交于 2019-12-04 09:02:31
Sergio Tulentsev

This is the offending line:

validates_presence_of :password, :on => create

Change it to

validates_presence_of :password, :on => :create

Also, do look at suggestions that stackoverflow shows you when you write a question. Reading those suggestions prevents 95% of my questions.

Update

There's another line

<%= form for @user do |f| %>

Should be

<%= form_for @user do |f| %>

Now please go and triple check that you typed all the code as you should :)

I had this same problem, and a simple restart of my server solved it.

Your code in User model has a typo on secret.

self.password_hash = BCrypt::Engine.hash_secrete(password, password_salt)

It should be

self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
  def encrypt_password
    if password.present?
        self.password_salt = BCrypt::Engine.generate_salt
        self.password_hash = BCrypt::Engine.hash_secrete(password, password_salt)
end

You also forgot to add the end for the if statement. It should be

  def encrypt_password
    if password.present?
        self.password_salt = BCrypt::Engine.generate_salt
        self.password_hash = BCrypt::Engine.hash_secrete(password, password_salt)
    end
  end

I've solved this problem with installing bcrypt-ruby of 3.0.x version against default. Specify it on Gemfile:

gem 'bcrypt-ruby', '~> 3.0.0'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!