[英]Authenticated and unauthenticated routes for Devise
我使用 Rails 5 創建了一個應用程序。我的用戶身份驗證由 Devise gem 管理。
我需要為經過身份驗證的用戶和未經身份驗證的用戶設置不同的根路徑。 我遵循了此處給出的提示。 一切看起來都很簡單,但在登錄后,例如單擊我的“主頁”鏈接時,我仍然被重定向到正常的 root_path。
這是我的 route.rb 代碼:
authenticated :user do
root to: 'api/v1/private/reporting/dashboards/summaries#index', as: :authenticated_root
end
root to: 'landing#index', as: :root
這是我的導航欄中“主頁”鏈接的代碼:
- if api_v1_public_members_user_signed_in?
= link_to 'Home', authenticated_root_path
- else
= link_to 'Home', root_path
有人能發現我可能遺漏的東西嗎?
** 僅供參考“api_v1_public_members_user_signed_in?” 方法可能看起來不熟悉,但它是必需的,因為我正在命名我的 devise 控制器。 有關這方面的更多信息,請參見此處。
嘗試將經過身份驗證和未經身份驗證的根路徑包裝在devise_scope
下,並為它們分別命名:
devise_scope :user do
authenticated :user do
root to: 'api/v1/private/reporting/dashboards/summaries#index', as: :authenticated_root
end
unauthenticated :user do
root to: 'landing#index', as: :unauthenticated_root
end
end
然后,將您的視圖更改為:
- if api_v1_public_members_user_signed_in?
= link_to 'Home', authenticated_root_path
- else
= link_to 'Home', unauthenticated_root_path
devise 文檔為此提供了一個模式。
在routes.rb
:
unauthenticated do
root "user#index"
end
authenticated :wealth_manager do
root "user#secret", as: :authenticated_root
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.