1

I'm quite new with Ruby language and Rails. I'm currently building some simple user registration process. When I used these codes Rails throws: no implicit conversion from nil to string

Here's the original code:

require 'digest'

class User < ActiveRecord::Base
  before_save :encrypt_password

  protected
  def encrypt_password
    return if password.blank?
    password = encrypt(password)
  end

  def encrypt(string)
    Digest::SHA1.hexdigest(string)
  end
end

But it works if I changed this line password = encrypt(password), to self.password = encrypt(password). I'm just curious, what's wrong with the first code?

Bayu
  • 36
  • 6

1 Answers1

3

Ruby doesn't allow using implicit self. with the person= type of method.
That is because it considers you're setting a local variable. So it doesn't relies on self.

You need to explicitely specify self.password =.

Damien MATHIEU
  • 31,924
  • 13
  • 86
  • 94