这两个模块是用来处理用户登录的信息存储,代码是参考 rubyonrailstoturial 第 8 章的内容。不知道为什么当程序运行到 SessionHelper 中的 sign_in 方法的时候, user.update_attributes(...)这句总是提示 NameError (uninitializedconstant SessionsHelper::Users)错误,求教!
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
sign_in user
redirect_to user
else
flash.now[:error] = 'Invalid email/password combination' #Not quite right!
render 'new'
end
end
def destroy
end
end
module SessionsHelper
def sign_in(user)
remember_token = User.new_remember_token
cookies.permanent[:remember_token] = remember_token
self.current_user = user
user.update_attribute(:remember_token, Users.encrypt(remember_token))
end
def signed_in?
!current_user.nil?
end
def current_user=(user)
@current_user = user
end
def current_user
remember_token = User.encrypt(cookies[:remember_token])
@current_user ||= User.find_by(remember_token: remember_token)
end
end