重定向捕获模型中方法中的exception

我使用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中覆盖了该方法,以便我可以更改代码。

嗯,你可以放一个处理has_token错误的方法吗? 抛出,并告诉控制器重定向该确切的错误。 你的控制器里有这样的东西:

rescue_from OauthError::RecordNotFound, :with => :deny_access然后你可以把

 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 

这是你通常可以处理错误的方法。 您的确切代码会有所不同,因为这是我们必须要做的。