次のコードでは、params[:email] に @ 文字が含まれている場合、次の行 user = User.authenticate(params[:email], params[:password]) で「引数の数が間違っています (2 に対して 0)」と表示されます。@ 文字がなければ、すべて正常に動作します。解決方法が 1 つもわかりません。
user = User.authenticate("test@test.com", params[:password]) のような文字列として電子メールを送信しようとしましたが、エラーは発生しません。
誰でもそれを手伝ってもらえますか?
私のコード:
コントローラ:
class SessionsController < ApplicationController
def new
end
def create
user = User.authenticate(params[:email], 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
モデル:
class User < ActiveRecord::Base
attr_accessor :password
before_save :encrypt_password
validates_confirmation_of :password
validates_presence_of :password, :on => :create
validates_presence_of :email
validates_uniqueness_of :email
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
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
終わり