2012-03-23 60 views
0

我想使用Devise的控制器輔助方法signed_in?檢查用戶是否已經簽名,如果是的話我想重定向到一個特定的頁面。設計如何添加登錄檢查自定義會話控制器?

我試過這個,但signed_in?方法總是返回true,我該如何做這個工作?

class SessionsController < Devise::SessionsController 
    def new 
    redirect_to root_url 
    end 

    def create 
    if signed_in?(resource_name) 
     redirect_to where_-_want_to_url 
    else 
     resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#failure") 

     return sign_in_and_redirect(resource_name, resource) 
    end 
    end 

    def sign_in_and_redirect(resource_or_scope, resource=nil) 
    scope = Devise::Mapping.find_scope!(resource_or_scope) 
    resource ||= resource_or_scope 
    sign_in(scope, resource) unless warden.user(scope) == resource 
    return render :json => {:success => true, :redirect => stored_location_for(scope) || after_sign_in_path_for(resource)} 
    end 

    def failure  
    return render :json => { :success => false } 
    end 

end 
+0

你想用這個做什麼?你只是想顯示一個不同的**主頁**取決於用戶是否登錄? – Ashitaka 2012-03-23 20:22:46

+0

不,有一種情況是登錄頁面被刪除,但實際上用戶已經登錄。所以我想做服裝重定向 – Rn2dy 2012-03-23 20:24:00

回答

0

你想要住在幫手。

當用戶招牌式,節省了用戶的CURRENT_USER

self.current_user = user 

要檢查用戶是否在您簽署可以使用

def signed_in? 
    !current_user.nil? 
end 

從邁克爾·哈特爾http://ruby.railstutorial.org/chapters/sign-in-sign-out#sec:current_user

編輯:

對不起,我剛剛閱讀編輯。你想要Devise的。

def'signed_in?'如何?好像?

對於第一個實現,我建議您自己滾動以更好地理解,並讓您知道所有組件的快速定製位置。接下來你可以實現其他的方法。

0

你試過if user_signed_in?

現在,我真的不知道你是什麼努力來完成,但我不會這樣定義,在我的控制器路由。我會做這樣的事情:

# config/routes.rb 
authenticated :user do 
    root :to => where_I_want_to_url 
end 
root :to => 'welcome#index'