简体   繁体   English

RoR - SessionsController中的ArgumentError#创建错误的参数数量(1表示2)

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

I am new to RoR and I am following this tutorial on making a user authentication system from scratch: http://railscasts.com/episodes/250-authentication-from-scratch . 我是RoR的新手,我正在按照本教程从头开始创建用户身份验证系统: http//railscasts.com/episodes/250-authentication-from-scratch I've been hung up on this error message all weekend: 我整个周末都被这个错误消息挂断了:

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

Here is the code from my sessions 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

Here is the code from my user.rb model (The error message gives an extracted source on line #10 => def self.authenticate (email, password) 这是来自我的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     

end 结束

authenticate method in the model takes two parameters, but you're passing just one (a hash with two keys). 模型中的authenticate方法有两个参数,但是你只传递一个(带有两个键的哈希)。 Change it to: 将其更改为:

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

The problem is in this line: 问题出在这一行:

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

it should be 它应该是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SessionsController#中的Omniauth ArgumentError创建错误数量的参数(1表示2)Rails 4 - Omniauth ArgumentError in SessionsController#create wrong number of arguments (1 for 2) Rails 4 ActiveAdmin :: Devise :: SessionsController #creore中的ArgumentError - ArgumentError in ActiveAdmin::Devise::SessionsController#create ArgumentError(错误的参数数量(2为1))-RoR应用程序的“查找”和“创建” - ArgumentError (wrong number of arguments (2 for 1)) - `find' and `create' for RoR app SessionsController#create中的NoMethodError - NoMethodError in SessionsController#create SessionsController#create中的NoMethodError - NoMethodError in SessionsController#create 如何修复 ArgumentError:arguments 的编号错误(给定 0,预期为 1)- RoR - How to fix ArgumentError: wrong number of arguments (given 0, expected 1) - RoR Api :: SessionsController#create中的NoMethodError - NoMethodError in Api::SessionsController#create Devise :: SessionsController#create中的NameError - NameError in Devise::SessionsController#create UsersController#create中的ArgumentError; 参数数量错误(2代表0) - ArgumentError in UsersController#create; Wrong number of arguments (2 for 0) ArgumentError,创建操作中的参数数目错误(3为1) - ArgumentError, wrong number of arguments (3 for 1) in create action
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM