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?

2

1 Answer 1

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 =.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.