簡體   English   中英

Rails設計 - 為登錄的用戶和匿名用戶定義單獨的根路徑

[英]Rails Devise - Define separate root paths for signed in user and anonymous user

我希望signed_in用戶的root_pathdashboard ,而未登錄用戶的用戶是index

我正在使用Devise,我知道有一個幫助user_signed_in? 但我不明白如何將它用於此目的。

我該如何工作?

你可以用這個:

unauthenticated :user do
  root :to => 'main#index'
end

authenticated :user do
  # Rails 3 syntax
  # root :to => "main#dashboard"   
  # Rails 4 requires the 'as' option to give it a unique name
  root :to => "main#dashboard", :as => "authenticated_root"
end

這是Devise為重新生成經過身份驗證的用戶提供的機制。

root :to => 'main#index' config/routes.rb root :to => 'main#index'行是Rails在首次創建應用程序時在config/routes.rb放置的標准行。 您可以將其包裝在unauthenticated :user do .. end塊中,以確保它適用於未登錄的用戶。

這為所有用戶提供了明確的根路由,具體取決於他們是否已登錄。

您可以在應用程序控制器( application_controller.rb )中執行以下操作

protected
def authenticate_user!
if user_signed_in?
  # navigate the user to dashboard
else
  # redirect to index
end

然后可以使用before_filter從其他控制器調用此方法示例:

class SomeController < ApplicationController
  before_filter :authenticate_user!
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM