简体   繁体   English

如何处理rails3中的404/500等错误

[英]how to handle errors like 404 / 500 in rails3

Hey, I hope you can help me. 嘿,我希望你能帮助我。

I am trying to find a way to direct the user to the default error pages 404.html and 500.html in my public folder. 我试图找到一种方法将用户定向到我公共文件夹中的默认错误页面404.html和500.html。

So when there is a routing or nomethod error it should be directed there to. 因此,当存在路由或nomethod错误时,应该将其指向。 I already tried some stuff in my application controller but it didnt work. 我已经在我的应用程序控制器中尝试了一些东西,但它没有用。

Many thanks!! 非常感谢!!

Rails does this for you automatically when running in production mode. 在生产模式下运行时,Rails会自动为您执行此操作。 When you upload your application to a live server, Rails takes care of handling those exceptions and rendering the correct error pages with the correct header status. 当您将应用程序上传到实时服务器时,Rails负责处理这些异常并使用正确的标头状态呈现正确的错误页面。 If you're trying to see what those pages look like (for testing or something), just access them directly via http://localhost:3000/404.html 如果您正在尝试查看这些页面的外观(用于测试或其他内容),只需通过http://localhost:3000/404.html直接访问它们

Whenever you set up your Rails application on a live server (let's use Apache as an example), you give the site root as the /public folder in your application. 每当您在实时服务器上设置Rails应用程序时(让我们以Apache为例),您将站点root作为应用程序中的/public文件夹。 Then, whenever a request is made to that server address, Apache first looks in that public folder and tries to serve a static asset (this is a configurable option in [environment].rb ). 然后,每当向该服务器地址发出请求时,Apache首先查找该公用文件夹并尝试提供静态资产(这是[environment].rb的可配置选项)。 If it can't find the requested page, then the request is forwarded through the Ruby stack. 如果找不到请求的页面,则通过Ruby堆栈转发请求。

When in production mode, if Rails encounters an error that isn't handled (ie begin, rescue), it throws the error the whole way up to the stack, which then tells Apache (again, in my example) to render an appropriate error. 在生产模式下,如果Rails遇到未处理的错误(即开始,救援),它会将错误全部抛到堆栈,然后告诉Apache(在我的示例中再次)呈现适当的错误。

Here are some common errors that you'll see in development mode and what they render in production mode: 以下是您在开发模式中看到的一些常见错误以及它们在生产模式下呈现的内容:

ActiveRecord::RecordNotFound => 404 (page not found)
nil.method => 500 (server error) unless you turn off whiny nils
ActionController::RoutingError => 404 (page not found)

如果在生产模式下运行,则会自动发生 - 无需您手动执行此操作。

看一下这篇文章 ,重定向导致路由错误的所有请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM