2014-04-22 120 views
2
class ApplicationController < ActionController::Base 

    protect_from_forgery 

    skip_before_filter :authenticate_user! , :only => ["welcome#index"] 

    # before_filter :authenticate_user! :except => ["welocme#index"] 

    def after_sign_in_path_for(user) 
     # user_dashboard_index_path 
     user_dashboard_index_path 
    end 

    def after_sign_out_path_for(user) 
     welcome_index_path 
    end 

    after_filter :authenticate_admin! 

    def after_sign_in_path_for(admin) 
     admin_dashboard_index_path 
    end 

    def after_sign_out_path_for(admin) 
     welcome_index_path 
    end 

end 

管理員不應訪問用戶儀表板,同樣,用戶不應訪問管理儀表板。分別對用戶和管理員進行身份驗證

我該如何做到這一點?

+1

你是如何設置你的設計?你有沒有遇到任何障礙?設計維基有關於如何實現這樣的信息。 authenticate_ * before_filters應該可以幫助您限制訪問。如果沒有發生,那麼請發佈相關的設備設置代碼。 –

回答

0

我已經在我的項目完成:

protect_from_forgery with: :exception 

def after_sign_in_path_for(resource) 
if user_signed_in? 
    user_dashboard_index_path 
elsif admin_signed_in? 
    admin_dashboard_index_path 
else 
    xyz_path 
end 
end 

同爲註銷:

def after_sign_out_path_for(resource) 
if user_signed_in? 
    welcome_index_path 
elsif admin_signed_in? 
    welcome_index_path 
else 
    xyz_path 
end 
end 

認證:

在(歡迎/指數)

<% if user_signed_in? %> 
    contant_of user 
<% else %> 
    you are not authenticated #admin can not authenticate this page 
<% end %> 

希望這將是有益的

+0

它是在WelcomeController中引發錯誤SyntaxError#index –

+0

class ApplicationController contant_of用戶 <% else %> 你不需要身份驗證 <% end %> 高清after_sign_in_path_for(資源) 如果user_signed_in? user_dashboard_index_path elsif admin_signed_in? admin_dashboard_index_path 其他 welcome_index_path 結束 結束 高清after_sign_out_path_for(資源) 如果user_signed_in? welcome_index_path elsif admin_signed_in? welcome_index_path else welcome_index_path end end/*我使用的是正確的編碼*/ –

+0

您是否在使用devise gem? –

相關問題