2013-03-28 70 views
2

我安裝了設計,它工作正常,但重定向登錄。當用戶從應用程序的任何頁面登錄時,Devise會將用戶重定向到首頁,而不是在同一頁面上重定向。我試着實現設計的How_to解決方案,但沒有任何幫助。登錄重定向到首頁,而不是停留在同一頁

我application_controller寫道:

def after_sign_in_path_for(resource) 
current_user_path 
end 

但它給錯誤:

undefined local variable or method `current_user_path' for #<Devise::SessionsController:0xaeacc34> 

當我寫:

def after_sign_in_path_for(resource) 
current_user_path 
end 

它提供:

undefined method `user_url' 

這個問題的解決方案是什麼?任何身體都可以幫助嗎?

回答

1

你需要重定向到其他scenerio(錯誤或成功)的地方?通常在用戶登錄成功後使用after_sign_in_path_for。 SO:

如果您尚未覆蓋設計默認功能,請使用以下代碼將控件導航至特定的自定義頁面。 current_user也可以在此操作中訪問。

def after_sign_in_path_for(resource) 
    scope = Devise::Mapping.find_scope!(resource) 
    scope_path = :"#{scope}_root_path" 
    respond_to?(scope_path, true) ? send(scope_path) : root_url 
end 

更新: 另一個例子如下:

def after_sign_in_path_for(resource_or_scope) 
    current_user.admin? ? dashboard_admin_home_index_path : current_user.sign_in_count >= 1 ? "/home/dashboard" : "/#{current_user.role}/dashboard" 
    end 
+1

爲好,它重定向到主頁。我需要用戶留在他以前的同一頁面上。 – user2206724 2013-03-28 08:50:50

1

您可以在範圍上面的代碼中使用request.referer

def after_sign_in_path_for(resource_or_scope) 
    request.referer 
end 
+0

謝謝!我不知道爲什麼這沒有得到更多upvotes。在提出的所有其他解決方案中,這是唯一爲我工作的解決方案 – guero64 2016-11-28 18:24:45