我将被重定向到仅出现500、404、422错误的主页,这样做可以“捕获”所有错误并重定向到主页吗?
我试过了,但它适用于404错误。
match "*path" => redirect("/"), via: :get
谢谢 !
在您的路线文件中:
#routes.rb
get '*unmatched_route', to: 'application#raise_not_found'
在您的应用程序控制器中
#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
您可以在此处找到完整的资源
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句