Take the 2-minute tour ×

I honestly had no idea what to call this any other way (or what to actually look for).

So, In my ActiveRecord (...app/model/s/account.rb/) I have the following:

validates :username, :password, :email, :presence => true
validates :username, :email, :uniqueness => true
validates :username, :length => { :within => 5..30}
validates :password, :length => { :within => 6..128}
validates :password, :confirmation => true
validates :email, :format => { :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i }
before_save :encrypt_password


    def encrypt_password
        require "digest"
        @account.password = Digest::SHA1.hexdigest(@account.password)

And I get the error:

undefined method `password' for nil:NilClass

Why does it report @account as nil? Well, might be because @account doesn't exist outside the View/Controller, but I (honest to god) couldn't think of any other way of doing this.

P.S: I tried putting the encrypt_password inside the controller; it still didn't work (different error, though).

share|improve this question
have you tried using just ":password" ? –  Harry Joy Jan 7 '11 at 10:28
Yep. And that one failed, too.(syntax error, unexpected '=', expecting keyword_end) –  destiel starship Jan 7 '11 at 10:31
How about @password –  Stephen Doyle Jan 7 '11 at 10:33
try creating account variable from "form" where you submit data. ie:- @account = Account.new(params[:userform]) –  Harry Joy Jan 7 '11 at 10:33
@Stephen - That one didn't work! :( @Harry - I already have, but that one's in the controller though. And everything works fine, I can even store it unto the database, the only thing that's not working out is storing the password in an encrypted form. –  destiel starship Jan 7 '11 at 10:36
show 1 more comment

2 Answers

up vote 4 down vote accepted

Change the method to read:

def encrypt_password
  require 'digest'
  self.password = Digest::SHA1.hexdigest(self.password)

The instance variable @account, which you use in the controller and view, refers to an instance of the class Account, which is what you're working on in this file. So, presumably, you're doing something like this in the controller:

@account = Account.new(params)

The instance of class Account receives the 'save!' call and fires its before_save callback, where it needs to refer to itself as 'self', not @account (which is the name your controller has for it).

Side note: you should require 'digest' at the top of your class file rather than inside the method - there's no need to load the digest library every time you want to hash a password.

share|improve this answer
N.B. you don't have to prefix password with 'self' in the method, I just think it's clearer to explain that way. –  Mortice Jan 7 '11 at 10:48
Ah, I could kiss you! Thanks! :) –  destiel starship Jan 7 '11 at 10:56
add comment

You should try

 def self.hash_password(password = '')
      self.password = Digest::SHA1.hexdigest(password)
share|improve this answer
add comment

Your Answer


