问题
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?
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:
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
来源:https://stackoverflow.com/questions/25219164/is-it-possible-to-catch-ruby-on-rails-error-pages-and-do-my-own-error-handling