2016-08-04 59 views
0

我已經創建了一個使用Rails 5的應用程序。我的用戶驗證由Devise gem管理。設計的已驗證和未驗證路由

我需要爲經過身份驗證和未經身份驗證的用戶擁有不同的根路徑。我遵循給出的提示here。一切看起來很簡單,但登錄後,我仍然重定向到正常的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?'方法可能看起來不熟悉,但它是必需的,因爲我命名空間我的設計控制器。有關詳細信息,請參閱here

回答

0

嘗試包裝既您的身份驗證和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 
+0

也似乎並沒有按預期工作。我仍然被重定向到'unauthenticated_root_path'@PaulFioravanti – Herm