Nomethoderror Ruby on rails

时间:2015-05-08 09:52:26

标签: ruby-on-rails nomethoderror

I'm having a Nomethoderror specifically:

NoMethodError (undefined method `verify_password' for nil:NilClass):

When running the Ruby on rails server I get the above error

and when actually attempting to use the method:

NoMethodError: undefined method `password_hash' for #

class User < ActiveRecord::Base
    validates_presence_of :Username, :salt, :Password_hash

    def self.create_with_password(username, password)
      salt = SecureRandom.hex
      password_hash = self.generate_hash(password, salt)
      self.create(
        Username: username,
        salt: salt,
        Password_hash: password_hash
      )
    end

    def verify_password(password)
      self.password_hash == User.generate_hash(password, self.salt)
    end

    def self.generate_hash(password, salt)    
      digest = OpenSSL::Digest::SHA256.new
      digest.update(password)
      digest.update(salt)
      digest.to_s
    end

thats the code I'm using atm

I have managed to solve the ">NoMethodError: undefined method `password_hash' for # class User < ActiveRecord::Base validates_presence_of :Username, :salt, :Password_hash" problem

although the ">NoMethodError (undefined method `verify_password' for nil:NilClass):" still persists, the code

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  # protect_from_forgery with: :exception

  def home

  end

  def register
    render layout: 'sign'  
  end
  # post request
  def login
    @username = params[:Username]
    @password = params[:Password]

    user = User.where(username: @username).first

    if
      valid = user.verify_password(@password)
    else
      if valid
        session[:signed_in] = true
        session[:Username] = params[:Username]
        redirect_to '/home'
      else
        redirect_to '/Sign-in.html'
      end
      redirect_to '/Sign-in.html'
    end
  end  
  def logout
    reset_session 
    redirect_to '/home'
  end
end

I'm not 100% sure why the problem would persist if my models are working with proof that testing it in the console works perfectly

2.1.5 :001 > User.create_with_password('help', 'pls')
   (0.4ms)  begin transaction
  SQL (0.8ms)  INSERT INTO "users" ("Password_hash", "Username", "salt") VALUES (?, ?, ?)  [["Password_hash", "f9216ad1e04d111473435268e0afac3cfa66eb53eb95bd42a793c7515790b707"], ["Username", "help"], ["salt", "8697d61a06cf6b501823d991db978000"]]
   (11.6ms)  commit transaction
 => #<User id: 7, Username: "help", salt: "8697d61a06cf6b501823d991db978000", Password_hash: "f9216ad1e04d111473435268e0afac3cfa66eb53eb95bd42a7..."> 
2.1.5 :002 > user = _
 => #<User id: 7, Username: "help", salt: "8697d61a06cf6b501823d991db978000", Password_hash: "f9216ad1e04d111473435268e0afac3cfa66eb53eb95bd42a7..."> 
2.1.5 :003 > user
 => #<User id: 7, Username: "help", salt: "8697d61a06cf6b501823d991db978000", Password_hash: "f9216ad1e04d111473435268e0afac3cfa66eb53eb95bd42a7..."> 
2.1.5 :004 > user.verify_password('pls')
 => true 
2.1.5 :005 > 

http://ipt-dynaman.c9.io/Sign-in.html that site which im currently developing has this problem

0 个答案:

没有答案