RoR - ArgumentError in SessionsController # creates wrong number of arguments (1 to 2)

I'm new to RoR and I am following this tutorial on how to build a user authentication system from scratch: http://railscasts.com/episodes/250-authentication-from-scratch . I was hung up on this error message over the weekend:

ArgumentError in SessionsController#create: wrong number of arguments (1 for 2). 

      

Here is my session controller code:

class SessionsController < ApplicationController
  def new
  end

  def create
    user = User.authenticate(:email => params[:email], :password => params[:password])
    if user
        session[:user_id] = user.id
        redirect_to root_url, :notice => "Logged in!"
    else
        flash.now.alert = "Invalid email or password"
        render "new"
    end
  end
end

      

Here is the code from my model user.rb

(the error is given by the extracted source at line # 10 =>def self.authenticate (email, password)

class User < ActiveRecord::Base

attr_accessor :password
validates_confirmation_of :password
validates_presence_of :email
validates_uniqueness_of :email
validates_presence_of :password, :on => :create # needed to move line up from below to. Cannot encrypt password without validating password
before_save :encrypt_password

def self.authenticate (email, password)
    user = find_by_email(email)
    if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
        user
    else
        nil
    end
end

def encrypt_password
    if password.present?
        self.password_salt = BCrypt::Engine.generate_salt
        self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
    end
end     

      

end

+1


source to share


2 answers


authenticate

the method in the model takes two parameters, but you only pass one (a hash with two keys). Change it to:



User.authenticate(params[:email], params[:password])

      

+2


source


The problem is this line:

user = User.authenticate(:email => params[:email], :password => params[:password])

      



he should be

user = User.authenticate(params[:email], params[:password])

      

+1


source







All Articles