Redirecting when catching an exception in a method in a model

I use Authlogic-connect to connect various service providers. There is a method in 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

When a service provider has already been added, does it give an error as indicated in has_token? method and page breaks. I need to redirect the application to the same page and skip the error. How should I do it? I redefined the method in my own user.rb to change the code.

+3
source share
1 answer

Hmm, well, could you put a method that handles the error that has_token? throws and tells the controller to redirect this exact error. something like this in your controller:

rescue_from OauthError::RecordNotFound, :with => :deny_access then you can put



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

- :


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

. , , .

+2

Source: https://habr.com/ru/post/1782201/


All Articles