否有可能在rails错误页面上捕获ruby并进行我自己的错误处理

否有可能在rails错误页面上捕获ruby并进行我自己的错误处理

本文介绍了是否有可能在rails错误页面上捕获ruby并进行我自己的错误处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输入错误的表单登录详细信息时,我会显示此页面:

I get this page when incorrect form login details are entered:

如果凭据正确无误,则仅登录用户.如果凭据无效,则会显示此错误页面.如何捕获此页面并自己处理错误?例如.重定向到同一页面或将错误添加到我的错误数组中,而不是显示此页面?

When credentials are correct the user is just logged in. When they're invalid this error page comes up. How do I catch this page and handle the error myself? E.G. redirect to same page or add the error to my array of errors rather than have this page show up?

控制器:

class UserController < ApplicationController

  def index
  end

  def new
    @user = User.new
  end

  def create
     @user = User.new(params[:user])
     if @user.valid?


       user = Parse::User.authenticate(params[:user][:username], params[:user][:password])
       login user
       #login_permanent user if params[:session][:remember_me] == "1"
       redirect_to '/adminpanel/show'
     else
       flash.now[:error] = "Invalid email password combination"
       render 'new'
     end

  end
end

推荐答案

您可以将产生错误的行包装在begin ... rescue块中:

You can wrap the line that's producing the error in a begin ... rescue block:

begin
  # user = Parse::User.authenticate...
rescue Parse::ParseProtocolError => e
  # Handle error (error object is stored in `e`)
end

您还可以通过在ApplicationController中使用rescue_from来捕获未处理的异常/错误.

You can also catch unhandled exceptions/errors by using rescue_from in your ApplicationController.

rescue_from Parse::ParseProtoIError do |e|
  # Handle error
end

这篇关于是否有可能在rails错误页面上捕获ruby并进行我自己的错误处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 03:25