說明
我使用了Ruby on Rails的web應用程序的色器件寶石並按照本教程就如何處理這些登錄和退出之後將用戶重定向回他們以前的頁面。問題是,代碼似乎適用於登錄部分,但註銷始終將用戶重定向到root_path。重定向適用於登錄而不是註銷
問題
正如我跟着教程完全相同,並且重定向工程之一,我錯過了一個錯字,還是有代碼的其他地方被改寫這個代碼?
版本使用
紅寶石:紅寶石2.2.1p85(2015年2月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 != "https://stackoverflow.com/users/password/" &&
request.path != "https://stackoverflow.com/users/password/new" &&
request.path != "https://stackoverflow.com/users/password/edit" &&
request.path != "https://stackoverflow.com/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
路由文件:後果在ApplicationHelper文件
## 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
沒有。
非常感謝您的幫助。
這可能是由於'after_sign_out_path_for(資源)' – Cyzanfar
我同意。它看起來像它會與這段代碼。但是,它完全從教程中複製,並且對於Devise gem也是有效的。在註銷期間是否有可能在ApplicationController的頂部沒有調用此代碼? 'after_filter:store_location'我不確定'after_filter'何時正常運行。 –