2010-11-25 4 views
0

사용자가 로그인하거나 로그 아웃 할 때마다 내 사용자 테이블의 상태 속성을 "온라인"또는 "오프라인"으로 변경하고 싶습니다. 내가 관련 방법에 update_attribute를 추가하여 내 자신의 세션 컨트롤러를 만들어 : Rails 3 + Devise : 로그인 할 때 데이터베이스 속성 업데이트

class SessionsController < Devise::SessionsController 
    def sign_out_and_redirect(resource_or_scope) 
    current_user.update_attribute(:status, "Offline") 
    scope = Devise::Mapping.find_scope!(resource_or_scope) 
    Devise.sign_out_all_scopes ? sign_out : sign_out(scope) 
    redirect_for_sign_out(scope) 
    end 

    def redirect_for_sign_out(scope) 
    redirect_to after_sign_out_path_for(scope)  
    end 

    def sign_in_and_redirect(resource_or_scope, *args) 
    options = args.extract_options! 
    scope = Devise::Mapping.find_scope!(resource_or_scope) 
    resource = args.last || resource_or_scope 

    if warden.user(scope) == resource 
     expire_session_data_after_sign_in! 
    else 
     sign_in(scope, resource, options) 
    end 

    current_user.update_attribute(:status, "Online") 

    redirect_for_sign_in(scope, resource) 
    end 

    def redirect_for_sign_in(scope, resource) 
    redirect_to stored_location_for(scope) || after_sign_in_path_for(resource) 
    end 

end 

및 대한

의 I가 정의되지 않은 방법 오류 얻을 다음 : 나는이 문제를 해결 어떻게

Devise.sign_out_all_scopes 

expire_session_data_after_sign_in! 

를?

sign_out(scope) 

를 한 다음은 로그 아웃 작동 : 내가 처음 정의되지 않은 메서드를 제거하면

난 단지있다. 하지만 로그인 용 표현을 삭제하고 싶지 않습니다.

어떻게해야합니까? 사용자의 온라인 상태를 추적하는 또 다른 방법이 있습니까?

답변

0

그냥 모든 것을 sessions_controller.rb에서 도우미/sessions_helper.rb로 옮기고 sign_out_all_scopes 및 expire_session_data_after_sign_in 메서드를 추가하여 문제를 해결했습니다! Devise API에서 도우미로.

+0

알았어. 내 문제가 해결 된 줄 알았는데 몇 시간 밖에 걸리지 않았어. 잠시 후에 작업이 중단되고 상태 속성이 nil이되었다. 새 사용자가 생성되거나 사용자 로그인 또는 로그 아웃 – Max

관련 문제