2012-02-18 174 views
11

我想爲Rails中的用戶顯示不同的根頁面。登錄用戶的不同頁面以及登錄用戶的根目錄

我定義根:

root :to => 'welcome#index' 

和歡迎控制:

class WelcomeController < ApplicationController 
    before_filter :authenticate_user! 

    def index 
    end 

end 

目前它是確定爲已登錄的用戶,但在重定向到/用戶用戶未登錄/ sign_in

我想顯示靜態根頁面,而不是重定向。

+1

刪除您的before_filter,並添加您的邏輯到索引方法來確定顯示基於它們是否在登錄你在使用設計? – 2012-02-18 07:49:21

+0

@MarcTalbot是的,是否有可能在routes.rb中實現? – 2012-02-18 07:51:02

+1

這個確切的問題已經在這裏得到解答http://stackoverflow.com/questions/8888289/rails-3-w-devise-how-to-set-two-separate-homepages-based-on-whether-the-user- i/8888513#8888513 – 2012-02-18 08:09:01

回答

2

這個答案應該可以工作。這是張貼在頁面布拉德利鏈接。

將其放入您的歡迎使用控制器中。

def index 
    if authenticate_user? 
    redirect_to :controller=>'dashboard', :action => 'index' 
    else 
    redirect_to '/public/example_html_file.html' 
    end 
end 
2

在你routes.rb

authenticated do 
    root :to => 'welcome#index' 
end 

root :to => 'home#static_page' 

這將確保root_url所有身份驗證的用戶是welcome#index

供您參考:https://github.com/plataformatec/devise/pull/1147

+0

這不再適用於Rails 4,請嘗試:通過身份驗證:用戶做到 root至:'dashboard#index',如::authenticated_root end root to:'landing_page#index',as :: public_root – ardochhigh 2015-11-10 10:40:09

23

答案,通過普尼特戈亞爾建議將不適用於Rails 4.請參閱this。該解決方案是使用的別名兩條路線是這樣的一個:

authenticated do 
    root :to => 'welcome#index', as: :authenticated 
end 

root :to => 'home#static_page' 
+1

Jose Valim在設計問題[這裏]討論這個解決方案(https://github.com/plataformatec/devise/issues/2393#issuecomment-17298414) – 2015-05-15 15:39:16

相關問題