重定向适用于登录但不适用于注销
说明
我正在使用带有Ruby on Rails webapp的devise gem,并按照本教程介绍如何在用户登录和注销后将用户重定向回上一页。 问题是,代码似乎适用于登录部分,但注销总是将用户重定向到root_path。
题
当我完全按照教程,并且重定向适用于一个,我是否错过了一个错字或者其他地方的代码是否覆盖了这段代码?
使用的版本
Ruby:ruby 2.2.1p85(2015-02-26修订版49769)[x86_64-darwin14]
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
路由文件:
## 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文件中没有任何后果。
预先感谢您的帮助。
我相信你的会话变量在注销时被设置为nil,因此重定向到根路径。
尝试使用以下设置保持会话范围与注销无关。
config.sign_out_all_scopes = false
在devise.rb文件中获取所需的行为
ref: 停止设置清算
您可以自定义after_sign_out_path_for(resource)
如果您想在用户注销后将用户重定向回上一页,如下所示
def after_sign_out_path_for(resource) request.referrer end
- 用户has_many:用户,:通过=>:朋友 – 怎么样?
- 在named_scope中通过2个带有条件的表进行排序
- Rails使用某些标签查找所有post而不使用acts_as_taggable
- 如何在Windows上为ruby安装’byebug’?
- 如何在Rails中自动加载猴子补丁代码?
- ror中的多个布局
- Rails 3 ActiveRecord:UNION
- 如何将remote_form_for的:update =>’div_id’选项从Rails 2升级到Rails 3?
- 使用Ruby 1.8.7的未定义方法`has_attached_file’,带有paperclip 2.3.8 gem for Rails 2