重定向用于登录但不注销 [英] Redirect works for Login but not Logout

查看:255
本文介绍了重定向用于登录但不注销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用devise gem与Ruby on Rails webapp,并在本教程之后介绍如何在用户登录后将用户重定向到之前的页面,登出。问题是,代码似乎适用于登录部分,但注销总是将用户重定向到root_path。

I am using the devise gem with a Ruby on Rails webapp and following this tutorial on how to redirect the user back to their previous page after they login and logout. The problem is, that the code seems to work for the login part, but logout always redirects the user to the root_path.

当我完全遵循教程,重定向工作在一个,我错过了一个打字错误,还是在其他代码覆盖这个代码?

As I followed the tutorial exactly, and the redirect works for one, have I missed a typo or is there code elsewhere that is overwriting this code?

Ruby:ruby 2.2.1p85(2015-02-26修订版49769)[x86_64-darwin14]

Ruby: ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-darwin14]

Rails:Rails 4.2.0

Rails: Rails 4.2.0

设计:3.4.1

应用程序控制器:

## app/controllers/application_controller.rb

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  after_filter :store_location
  before_action :configure_permitted_parameters, if: :devise_controller?

  def store_location
    # store last url - this is needed for post-login redirect to whatever the user last visited.
    return unless request.get? 
    if (request.path != "/login" &&
        request.path != "/logout" &&
        request.path != "/register" &&
        request.path != "/users/password/" &&
        request.path != "/users/password/new" &&
        request.path != "/users/password/edit" &&
        request.path != "/users/confirmation" &&
        request.path != "/profile/" &&
        request.path != "/profile/edit" &&
        request.path != "/admin/dashboard" &&
        request.path != "/admin/moderate_users" &&
        request.path != "/admin/moderate_events" &&
        request.path != "/admin/moderate_event_items" &&
        request.path != "/admin/moderate_companies" &&
        request.path != "/admin/moderate_locations" &&
        request.path != "/admin/moderate_stories" &&
        !request.xhr?) # don't store ajax calls
      session[:previous_url] = request.fullpath 
    end
  end

  protected

  def after_sign_in_path_for(resource)
   session[:previous_url] || root_path
  end

  def after_sign_out_path_for(resource)
    session[:previous_url] || root_path
  end

end

路由文件: p>

The Routes file:

## app/config/routes.rb

Rails.application.routes.draw do

  ## Site's Root Route
  root 'pages#home'

  ## Static Page Routes
  get 'home' => 'pages#home'
  get 'about' => 'pages#about'
  get 'contact' => 'pages#contact'
  get 'privacy' => 'pages#privacy'
  get 'sitemap' => 'pages#sitemap'

  ## Administrative Routes
  get 'admin/dashboard'
  get 'admin/moderate_users'
  get 'admin/moderate_events'
  get 'admin/moderate_event_items'
  get 'admin/moderate_companies'
  get 'admin/moderate_locations'
  get 'admin/moderate_stories'

  ## Customed Devise Routes
  devise_for  :users, 
              :skip => [:sessions, :registrations]

  devise_scope :user do
    get    "login",               to: "devise/sessions#new",            as: :new_user_session
    post   "login",               to: "devise/sessions#create",         as: :user_session
    delete "logout",              to: "devise/sessions#destroy",        as: :destroy_user_session

    get    "register",            to: "devise/registrations#new",       as: :new_user_registration
    post   "register",            to: "devise/registrations#create",    as: :user_registration
    get    "account/delete",      to: "devise/registrations#cancel",    as: :cancel_user_registration

    get    "user/profile/edit",   to: "devise/registrations#edit",      as: :edit_user_registration

    patch  "user",                to: "devise/registrations#update"
    put    "user",                to: "devise/registrations#update"
    put    "register",            to: "devise/registrations#update"
    delete "user/delete",         to: "devise/registrations#destrony"

    get    "user/profile",        to: 'devise/registrations#edit',      as: :user_root
  end

end

没有任何后果ApplicationHelper文件。

Nothing of consequence in the ApplicationHelper file.

提前谢谢您的帮助。

推荐答案

我相信您的会话变量在注销时被设置为nil,因此重定向到根路径。

I believe your session variable is being set to nil on logout, hence the redirect to the root path.

尝试以下设置保持会话范围与退出无关。

Try the below setting to the keep session scope irrelevant to logout.


config.sign_out_all_scopes = false

在devise.rb文件中获取所需的行为

in the devise.rb file to get the desired behaviour

ref:停止设计清算会话

这篇关于重定向用于登录但不注销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆