Is it possible to catch ruby on rails error pages

2019-08-28 17:56发布

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

enter image description here

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?

Controller:

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

1条回答
Bombasti
2楼-- · 2019-08-28 18:43

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

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

rescue_from Parse::ParseProtoIError do |e|
  # Handle error
end
查看更多
登录 后发表回答