Redirect works for Login but not Logout

廉价感情. 提交于 2019-12-08 08:20:34

问题


Explanation

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.

Question

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?

Versions Used

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

Rails: Rails 4.2.0

Devise: 3.4.1

Code

The Application Controller:

## 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

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

Nothing of consequence in the ApplicationHelper file.

Thank you in advance for your help.


回答1:


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

in the devise.rb file to get the desired behaviour

ref: Stop Devise from clearing session




回答2:


You can customize after_sign_out_path_for(resource) if you want to redirect the user back to their previous page after they logout like below

def after_sign_out_path_for(resource)
   request.referrer
end


来源:https://stackoverflow.com/questions/31361265/redirect-works-for-login-but-not-logout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!