modelに独自に配置したメソッドauthenticateをcontrollerから呼び出せません。
NoMethodError (undefined method
authenticate' for #<Class:0x007ffd3aa68008>): app/controllers/sessions_controller.rb:3:in
create'
というエラーが出力されます。
どなたか原因お分かりになりますでしょうか?
controllerとmodelは以下です。
・controller
class SessionsController < ApplicationController
def create
user = User.authenticate(params[:email],params[:password])
if user
session[:user_id] = user.id
else
flash.alert = "メールアドレスとパスワードが一致しません"
end
redirect_to root
enddef destroy
session.delete(:user_id)
redirect_to root
enddef login
end
end
・model
class User < ActiveRecord::Base
validates :email,
format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})\Z/i, allow_blank: false,on: :create},
uniqueness: {message: :overlapping_email}
validates :name,
length: { maximum: 30 },
format: { with: /\A[a-z0-9]+\z/i,allow_blank: false,message: :invalid_name},
uniqueness: {message: :overlapping_name}
validates :password,
length: { maximum: 30 },
format: { with: /\A[a-z0-9]+\z/i,allow_blank: false,message: :invalid_password}attr_accessor :password
def password=(val)
if val.present?
self.hashed_password = BCrypt::Password.create(val)
end
@password = val
enddef authenticate(email,password)
user = find_by(email: email)
if user && user.hashed_password.present? &&
BCrypt::Password.new(user.hashed_password) == password
user
else
nil
end
end
end
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/10/19 07:24