我正在阅读第 8.2 节 Michael Hartl 的 RoR 3.2 教程,似乎无法让用户登录。我完全按照教程进行操作,但由于某种原因,当我调试并打印出来时,@current_user 似乎是空白的提交登录表单后的视图。我已经多次查看代码,似乎找不到任何错误。任何帮助表示赞赏。谢谢你。
module SessionsHelper
def sign_in(user)
cookies.permanent[:remember_token] = user.remember_token
self.current_user = user
end
def signed_in?
!current_user.nil?
end
def current_user=(user)
@current_user = user
end
def current_user
@current_user ||= User.find_by_remember_token(cookies[:remember_token])
end
def sign_out
self.current_user = nil
cookies.delete(:remember_token)
end
end
我的 session_controller.rb
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 new_job_url
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
end
def destroy
sign_out
redirect_to root_url
end
结尾