簡體   English   中英

RoR - SessionsController中的ArgumentError#創建錯誤的參數數量(1表示2)

[英]RoR - ArgumentError in SessionsController#create wrong number of arguments (1 for 2)

我是RoR的新手,我正在按照本教程從頭開始創建用戶身份驗證系統: http//railscasts.com/episodes/250-authentication-from-scratch 我整個周末都被這個錯誤消息掛斷了:

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

這是我的會話控制器的代碼:

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

這是來自我的user.rb模型的代碼(錯誤消息在第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     

結束

模型中的authenticate方法有兩個參數,但是你只傳遞一個(帶有兩個鍵的哈希)。 將其更改為:

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

問題出在這一行:

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

它應該是

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM