title: Ruby on Rails: BCrypt InvalidSalt Error
body: <p>I'm new to rails (and ruby in general), so my problem is probably easy to solve. I'm trying to create a simple app where you can create a user and log in. I'm encrypting the password with BCrypt and when i try to log in i get this error: <code>BCrypt::Errors::InvalidSalt in SessionsController#login_attempt</code></p>
  
  <p>Not sure what files i need to share to solve the problem, so i'll start by sharing the files where it says the error occours.</p>
  
  <p><strong>user.rb</strong></p>
  
  <pre><code>    class User &lt; ActiveRecord::Base
          before_save :encrypt_password
          after_save :clear_password
  
          attr_accessor :password
          attr_accessible :username, :email, :password, :password_confirmation
  
          EMAIL_REGEX = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}$/i
          validates :username, :presence =&gt; true, :uniqueness =&gt; true, :length =&gt; { :in =&gt; 3..20 }
          validates :email, :presence =&gt; true, :uniqueness =&gt; true, :format =&gt; EMAIL_REGEX
          validates :password, :confirmation =&gt; true #password_confirmation attr
          validates_length_of :password, :in =&gt; 6..20, :on =&gt; :create
  
          def encrypt_password
              if :password.present?
                  self.salt = BCrypt::Engine.generate_salt
                  self.encrypted_password= BCrypt::Engine.hash_secret(:password, :salt)
              end
          end
  
          def clear_password
              self.password = nil
          end
  
          def self.authenticate(username_or_email="", login_password="")
              if  EMAIL_REGEX.match(username_or_email)    
                  user = User.find_by_email(username_or_email)
              else
                  user = User.find_by_username(username_or_email)
              end
  
              if user &amp;&amp; user.match_password(login_password)
                  return user
              else
                  return false
              end
          end
  
          def match_password(login_password="")
              encrypted_password == BCrypt::Engine.hash_secret(login_password, salt)
          end
      end  
  </code></pre>
  
  <p><strong>session_controller.rb</strong></p>
  
  <pre><code>class SessionsController &lt; ApplicationController
  
    before_filter :authenticate_user, :only =&gt; [:home, :profile, :setting]
    before_filter :save_login_state, :only =&gt; [:login, :login_attempt]
  
    def login
      #Login Form
    end
  
    def login_attempt
      authorized_user = User.authenticate(params[:username_or_email],params[:login_password])
      if authorized_user
        flash[:notice] = "Wow Welcome again, you logged in as #{authorized_user.username}"
        redirect_to(:action =&gt; 'home')
      else
        flash[:notice] = "Invalid Username or Password"
        flash[:color]= "invalid"
        render "login"    
      end
    end
  
    def home
    end
  
    def profile
    end
  
    def setting
    end
  
    def logout
    session[:user_id] = nil
    redirect_to :action =&gt; 'login'
    end
  end
  </code></pre>
  
  <p>I followed a tutorial to get this far, so if you can please explain the error too.</p>
  
  <p>Thanks!</p>