【发布时间】:2016-08-20 22:22:54
【问题描述】:
我是 Rails 新手,在尝试破坏用户会话时遇到了麻烦。 我的会话控制器看起来像这样
class SessionsController < ApplicationController
def new
end
def create
name = params[:name]
password = params[:password]
user = User.authenticate(name, password)
if user.nil?
render json: {isLogin: false}
else
session[:user_id] = user.id
render json: {isLogin: true}
end
end
def destroy
session[:user_id] = nil
puts session[:user_id] # Nothing gets printed to the console here
render json: {isLogin: false}
end
end
当我调用 'sessions/destroy' 并尝试销毁会话时,'puts session[:user_id]' 行不会打印任何内容。所以我确定当时会话为零。但问题是即使在我为该用户销毁会话之后,我仍然可以从不同的控制器访问这样的会话。
class LessonsController < ApplicationController
def getLesson
userId = session[:user_id]
# do stuff
end
end
为什么会这样?我该如何解决这个问题?。
【问题讨论】:
-
路线怎么称呼?你是如何定义路线的?在
getLesson;它打印什么? -
嗯.. 在 ruby 控制台上尝试“puts nil”,打印的是什么? ;)
标签: ruby-on-rails session