gpt4 book ai didi

ruby-on-rails - RoR - SessionsController 中的 ArgumentError#create 错误数量的参数(1 对 2)

转载 作者:太空宇宙 更新时间:2023-11-03 18:19:08 26 4
gpt4 key购买 nike

我是 RoR 的新手,我正在按照本教程从头开始制作用户身份验证系统:http://railscasts.com/episodes/250-authentication-from-scratch .我整个周末都被这条错误消息挂断了:

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

这是我的 session Controller 的代码:

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

这是来 self 的 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])

关于ruby-on-rails - RoR - SessionsController 中的 ArgumentError#create 错误数量的参数(1 对 2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21221888/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com