RoR – SessionsController中的ArgumentError#创建错误的参数数量(1表示2)
我是RoR的新手,我正在按照本教程从头开始创建用户身份validation系统: http : //railscasts.com/episodes/250-authentication-from-scratch 。 我整个周末都被这个错误信息挂断了:
ArgumentError in SessionsController#create: wrong number of arguments (1 for 2).
这是我的会话控制器的代码:
class SessionsController 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 :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])