Ruby on rails 会话[:user_id]正在从nil更改为浏览器返回按钮上的上一个值
在我的rails应用程序中,当我注销时,在Ruby on rails 会话[:user_id]正在从nil更改为浏览器返回按钮上的上一个值,ruby-on-rails,session,Ruby On Rails,Session,在我的rails应用程序中,当我注销时,在destroy方法中,我正在设置session[:user\u id]=nil。但当我按下浏览器上的“后退”按钮时,会话[:user\u id]将返回其以前的值,并自动显示登录页面。为什么会这样?如何使会话[:user_id]=nil保持不变,直到我更改它 会话_controller.rb class SessionsController < ApplicationController def index end def show
destroy
方法中,我正在设置session[:user\u id]=nil
。但当我按下浏览器上的“后退”按钮时,会话[:user\u id]
将返回其以前的值,并自动显示登录页面。为什么会这样?如何使会话[:user_id]=nil
保持不变,直到我更改它
会话_controller.rb
class SessionsController < ApplicationController
def index
end
def show
end
def new
end
def create
@user = User.find_by_email(params[:email])
if @user && @user.authenticate(params[:password])
session[:user_id] = @user.id
redirect_to user_posts_path
else
render 'new'
end
end
def destroy
session[:user_id] = nil
end
end
在注销操作中使用
reset_session
。这将发出一个新的会话标识符,并声明旧的标识符无效,并防止其他基于会话固定的攻击
这是如何正确设置SessionController的完整演示:
会话实际上不像标准的crud资源,您可以在其中使用全套crud谓词并从数据库中获取记录
从用户的角度来看,只有三个操作:
new - displays the login form
create - verifies the credentials and signs the user in.
destroy - logs user out by resetting the session.
更改路由定义以将会话视为单一资源:
resource :sessions, only: [:new, :create, :destroy]
然后我们将创建一个帮助器:
module SessionsHelper
def current_user
@user ||= User.find!(session[:user_id]) if session[:user_id]
end
def user_signed_in?
!current_user.nil?
end
def can_sign_in?
user_signed_in? || current_page?(new_user_path) || current_page?(new_session_path)
end
end
这样,用户在会话中的存储方式的实际实现仅在应用程序中的一个位置,而不是遍布控制器和视图
让我们确保可以从控制器调用它:
class ApplicationController < ActionController::Base
include SessionsHelper
end
由于应用程序布局往往会变成怪物,因此我们使用局部布局。
sessions/\u actions.html.erb
<% if user_signed_in? %>
Logged in as <%= current_user.email %>
<%= link_to 'Log Out', session_path, method: :delete %>
<% else %>
<%= link_to 'Log in', new_session_path if can_sign_in? %>
<% end %>
登录为
在注销操作中使用重置会话
。这将发出一个新的会话标识符,并声明旧的标识符无效,并防止其他基于会话固定的攻击
这是如何正确设置SessionController的完整演示:
会话实际上不像标准的crud资源,您可以在其中使用全套crud谓词并从数据库中获取记录
从用户的角度来看,只有三个操作:
new - displays the login form
create - verifies the credentials and signs the user in.
destroy - logs user out by resetting the session.
更改路由定义以将会话视为单一资源:
resource :sessions, only: [:new, :create, :destroy]
然后我们将创建一个帮助器:
module SessionsHelper
def current_user
@user ||= User.find!(session[:user_id]) if session[:user_id]
end
def user_signed_in?
!current_user.nil?
end
def can_sign_in?
user_signed_in? || current_page?(new_user_path) || current_page?(new_session_path)
end
end
这样,用户在会话中的存储方式的实际实现仅在应用程序中的一个位置,而不是遍布控制器和视图
让我们确保可以从控制器调用它:
class ApplicationController < ActionController::Base
include SessionsHelper
end
由于应用程序布局往往会变成怪物,因此我们使用局部布局。
sessions/\u actions.html.erb
<% if user_signed_in? %>
Logged in as <%= current_user.email %>
<%= link_to 'Log Out', session_path, method: :delete %>
<% else %>
<%= link_to 'Log in', new_session_path if can_sign_in? %>
<% end %>
登录为
在注销操作中使用重置会话
。这将发出一个新的会话标识符,并声明旧的标识符无效,并防止其他基于会话固定的攻击
这是如何正确设置SessionController的完整演示:
会话实际上不像标准的crud资源,您可以在其中使用全套crud谓词并从数据库中获取记录
从用户的角度来看,只有三个操作:
new - displays the login form
create - verifies the credentials and signs the user in.
destroy - logs user out by resetting the session.
更改路由定义以将会话视为单一资源:
resource :sessions, only: [:new, :create, :destroy]
然后我们将创建一个帮助器:
module SessionsHelper
def current_user
@user ||= User.find!(session[:user_id]) if session[:user_id]
end
def user_signed_in?
!current_user.nil?
end
def can_sign_in?
user_signed_in? || current_page?(new_user_path) || current_page?(new_session_path)
end
end
这样,用户在会话中的存储方式的实际实现仅在应用程序中的一个位置,而不是遍布控制器和视图
让我们确保可以从控制器调用它:
class ApplicationController < ActionController::Base
include SessionsHelper
end
由于应用程序布局往往会变成怪物,因此我们使用局部布局。
sessions/\u actions.html.erb
<% if user_signed_in? %>
Logged in as <%= current_user.email %>
<%= link_to 'Log Out', session_path, method: :delete %>
<% else %>
<%= link_to 'Log in', new_session_path if can_sign_in? %>
<% end %>
登录为
在注销操作中使用重置会话
。这将发出一个新的会话标识符,并声明旧的标识符无效,并防止其他基于会话固定的攻击
这是如何正确设置SessionController的完整演示:
会话实际上不像标准的crud资源,您可以在其中使用全套crud谓词并从数据库中获取记录
从用户的角度来看,只有三个操作:
new - displays the login form
create - verifies the credentials and signs the user in.
destroy - logs user out by resetting the session.
更改路由定义以将会话视为单一资源:
resource :sessions, only: [:new, :create, :destroy]
然后我们将创建一个帮助器:
module SessionsHelper
def current_user
@user ||= User.find!(session[:user_id]) if session[:user_id]
end
def user_signed_in?
!current_user.nil?
end
def can_sign_in?
user_signed_in? || current_page?(new_user_path) || current_page?(new_session_path)
end
end
这样,用户在会话中的存储方式的实际实现仅在应用程序中的一个位置,而不是遍布控制器和视图
让我们确保可以从控制器调用它:
class ApplicationController < ActionController::Base
include SessionsHelper
end
由于应用程序布局往往会变成怪物,因此我们使用局部布局。
sessions/\u actions.html.erb
<% if user_signed_in? %>
Logged in as <%= current_user.email %>
<%= link_to 'Log Out', session_path, method: :delete %>
<% else %>
<%= link_to 'Log in', new_session_path if can_sign_in? %>
<% end %>
登录为
请添加服务器日志和控制器。我怀疑你的行为可能根本就不会被执行。@max:编辑了这个问题你在这里有很多奇怪的地方。会议应该是单一的资源。因此,注销路径应该是没有id的DESTROY/sessions
。为什么有sessions/DESTROY.html.erb
视图?您应该改为重定向-使用flash消息通知用户。请添加服务器日志和控制器。我怀疑你的行为可能根本就不会被执行。@max:编辑了这个问题你在这里有很多奇怪的地方。会议应该是单一的资源。因此,注销路径应该是没有id的DESTROY/sessions
。为什么有sessions/DESTROY.html.erb
视图?您应该改为重定向-使用flash消息通知用户。请添加服务器日志和控制器。我怀疑你的行为可能根本就不会被执行。@max:编辑了这个问题你在这里有很多奇怪的地方。会议应该是单一的资源。因此,注销路径应该是没有id的DESTROY/sessions
。为什么有sessions/DESTROY.html.erb
视图?您应该改为重定向-使用flash消息通知用户。请添加服务器日志和控制器。我怀疑你的行动可能根本就没有执行。@max:编辑了这个问题你有很多奇怪的地方