Is it possible to catch ruby on rails error pages and do my own error handling?

杀马特。学长 韩版系。学妹 提交于 2019-12-11 14:34:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!