重定向404、422、500 Ruby On rails [英] Redirect 404, 422, 500 Ruby On rails
本文介绍了重定向404、422、500 Ruby On rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我会被重定向到仅出现500、404、422错误的首页,这样做可以捕获"所有错误并重定向到首页吗?
I would be redirected to the homepage of only 500, 404, 422 error appears, do-it is possible to "catch" all errors and redirect to homepage?
我尝试过,但是它可以解决404错误.
I tried that but it works for the 404 error.
match "*path" => redirect("/"), via: :get
谢谢!
推荐答案
在您的路由文件中:
#routes.rb
get '*unmatched_route', to: 'application#raise_not_found'
在您的应用程序控制器中
In your application controller
#application_controller.rb
rescue_from ActiveRecord::RecordNotFound, with: :not_found
rescue_from Exception, with: :not_found
rescue_from ActionController::RoutingError, with: :not_found
def raise_not_found
raise ActionController::RoutingError.new("No route matches #{params[:unmatched_route]}")
end
def not_found
respond_to do |format|
format.html { render file: "#{Rails.root}/public/404", layout: false, status: :not_found }
format.xml { head :not_found }
format.any { head :not_found }
end
end
def error
respond_to do |format|
format.html { render file: "#{Rails.root}/public/500", layout: false, status: :error }
format.xml { head :not_found }
format.any { head :not_found }
end
end
您可以在此处
这篇关于重定向404、422、500 Ruby On rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文