2010-12-26 4 views
3

Authlogic-connect를 사용하여 다양한 서비스 제공자를 연결합니다. user.rb에 메소드가 있습니다.모델의 메소드에서 예외를 포착하는 방향으로 재 지정

def complete_oauth_transaction 
     token = token_class.new(oauth_token_and_secret) 
     old_token = token_class.find_by_key_or_token(token.key, token.token) 
     token = old_token if old_token 

     if has_token?(oauth_provider) 
     self.errors.add(:tokens, "you have already created an account using your #{token_class.service_name} account, so it") 
     else 
     self.access_tokens << token 
     end 
    end 

서비스 제공자가 이미 추가 된 경우 has_token? 메서드 및 페이지 나누기가 포함됩니다. 앱을 동일한 페이지로 리디렉션하고 오류를 플래시해야합니다. 어떻게해야합니까? 나는 내 자신의 user.rb에있는 메소드를 재정 의하여 코드를 바꿀 수있다.

답변

2

흠, has_token 오류를 처리하는 방법을 넣을 수 있습니까? throw하고 컨트롤러에 정확한 오류를 리디렉션하도록 지시합니다.


if complete_oauth_transaction.errors.present? 
    redirect_to your_view_path 
else 
    # continue on with the normal code here 
end 

이것은 당신이 일반적으로 오류를 처리 할 수있는 방법은 다음과 같습니다 컨트롤러에서이 같은 일이 :

다음

rescue_from OauthError::RecordNotFound, :with => :deny_access 당신은



def deny_access 
    redirect_to your_view_path, :alert => "Too bad sucker" #some flash message 
end 

를 넣을 수 있습니다 또는 당신은 컨트롤러에서 이런 일을 할 수 . 귀하의 정확한 코드는 달라질 수 있습니다.

관련 문제