设计:注销*没有*重定向?
Posted
技术标签:
【中文标题】设计:注销*没有*重定向?【英文标题】:Devise: Logout *without* redirecting? 【发布时间】:2013-10-23 11:15:18 【问题描述】:我正在尝试使用 devise 注销但没有重定向!所以我想在注销后禁用重定向。这样做的原因是我想渲染某个模板。 如何在不重定向用户的情况下从设计中注销用户 --> 仅此方法 我已经覆盖了设计会话控制器。
我的方法(从 application_controller.rb 中的过滤器之前调用)
def check_concurrent_session
if user_signed_in?
if current_user && !(session[:token] == current_user.login_token)
render :template => "users/logout_duplex", :layout => "application", :locals => sub_layout => "left"
# The next line always redirects to signout_path
# I want to disable the complete redirect so it just logs out and shows the rendered template
Devise.sign_out_all_scopes ? sign_out : sign_out(@user)
return
end
end
end
【问题讨论】:
【参考方案1】:从设计继承会话控制器并拥有自己的after_sign_out_path_for
实现。这是设计用于在注销后重定向的方法。
class SessionsController < Devise::SessionsController
protected
def after_sign_out_path_for(resource)
//your implementation
end
end
将此添加到您的 config/routes.rb
devise_for :users, :controllers => :sessions => 'sessions'
该方法的设计实现如下所示
def after_sign_out_path_for(resource_or_scope)
respond_to?(:root_path) ? root_path : "/"
end
【讨论】:
我知道我可以覆盖设计控制器并且已经这样做了,但这不是问题的答案。我只想为我发布的这种方法禁用重定向,而不是在应用程序范围内(编辑问题以使其更清楚) 这个方法是什么意思?你是说那个动作? after_sign_out_path_for 是您可以执行此操作的方法。 是的,但它是应用程序范围内的,我只想 -1time- 退出而不重定向,现有的重定向位置由您建议的方法设置,我想保持不变 仅调用方法sign_out 不会重定向到root_path。我想我错过了一些东西。 github.com/plataformatec/devise/blob/master/app/controllers/…。该链接中的destroy方法调用signout,然后重定向到after_sign_out_path。 使用我示例中的行。设计.sign_out_all_scopes ? sign_out : sign_out(@user) does 使用 def after_sign_out_path 位置进行重定向,我不想发生这种情况,这就是为什么这个问题以上是关于设计:注销*没有*重定向?的主要内容,如果未能解决你的问题,请参考以下文章