[英]Setting custom after_sign_up_path_for in Devise gem
取決於用戶在注冊期間選擇的角色(例如,專家與新手),我想將他/她重定向到適當的頁面。
我嘗試覆蓋ApplicationController
的方法。 還嘗試覆蓋我的自定義RegistrationsController
:
def after_sign_up_path_for(resource)
if current_user.role = 'xxx'
redirect_to path1
else
redirect_to path2
end
end
我也嘗試過:
def after_sign_up_path_for(resource)
if current_user.role = 'xxx'
redirect_to path1
else
redirect_to path2
end
return
end
但該應用程序仍然重定向到默認的root_path
。
這是設計文檔供您參考
看着它,我認為你將不得不使用resource
(你正在使用current_user
):
def after_sign_up_path_for(resource)
path = (resource.role == "xx") ? path1 : path2
redirect_to path
end
這就是我解決的方法,覆蓋自定義控制器中的方法,更新routes.rb來映射它。
devise_for :users , :path=>'accounts', controllers: { registrations: "registration" }
控制器:
class RegistrationController < Devise::RegistrationsController
def after_sign_up_path_for(resource)
if current_user.role = 'xx'
new_expert_path
else
new_user_expertrequest
end
end
end
剛剛刪除了redirect_to,因為父控制器按如下方式調用此方法並執行重定向
# POST /resource <Devise:RegistrationsController>
def create
xxxx
if resource_saved
if resource.active_for_authentication?
xxxx
respond_with resource, location: after_sign_up_path_for(resource)
else
xxx
else
xxxx
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.