2015-07-11 60 views
1

說明

我使用了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 

沒有。

非常感謝您的幫助。

+0

這可能是由於'after_sign_out_path_for(資源)' – Cyzanfar

+0

我同意。它看起來像它會與這段代碼。但是,它完全從教程中複製,並且對於Devise gem也是有效的。在註銷期間是否有可能在ApplicationController的頂部沒有調用此代碼? 'after_filter:store_location'我不確定'after_filter'何時正常運行。 –

回答

1

我相信你的會話變量被設置爲在註銷爲零,因此重定向到根路徑。

請嘗試以下設置以保持與註銷無關的會話範圍。

config.sign_out_all_scopes = false 
在devise.rb文件

以獲得所需的行爲

裁判:Stop Devise from clearing session

+0

感謝您的建議。我在設計中設置了這個變量。rb文件,並重新啓動服務器,它現在肯定可以工作。謝謝!我的應用程序現在會將用戶返回到登錄和註銷時的上一頁。 (除非頁面受保護) –

+0

一個獨特的異常是,如果用戶在註銷時處於受限制的頁面上,它們不會像我期望的那樣被重定向到root_path,而是被重定向到前一頁他們在第一次登錄時就打開了。但是,當從不安全的頁面註銷時,即使它們不是他們登錄前所在的頁面,它們也會重定向到該頁面。這可能需要被張貼爲另一個問題。我會花一段時間去嘗試和解決它。再次感謝您@Rots的解決方案! –

+0

@ChristopherWarrington當涉及到路由和安全/不安全頁面時,選擇正確的頁面會出現問題。我相信你會弄明白的! – Rots

0

您可以自定義after_sign_out_path_for(resource)如果您想將用戶重定向回他們以前的網頁,他們註銷等之後,下面

def after_sign_out_path_for(resource) 
    request.referrer 
end 
+0

謝謝@Pavan,但是當引用的頁面被鎖定時,這對我不起作用。當用戶註銷時,他們將無法訪問該引用的頁面,並會在錯誤時將其踢入登錄頁面。我不希望剛退出的用戶被踢到登錄頁面,並告訴他們除非他們已登錄才能訪問頁面。我想保留代碼,因爲我擁有它, t知道爲什麼'def after_sign_out_path_for(resource)'部分在'def after_sign_in_path_for(resource)'時不起作用。謝謝你的建議。 –

相關問題