我会被重定向到主页,只有 500、404、422 错误出现,是否可以“捕获”所有错误并重定向到主页?
我试过了,但它适用于 404 错误。
match "*path" => redirect("/"), via: :get
谢谢!
最佳答案
在你的路由文件中:
#routes.rb
get '*unmatched_route', to: 'application#raise_not_found'
在你的应用程序 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
您可以找到完整的资源 here
关于ruby-on-rails - 重定向 404、422、500 Ruby On rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34889043/