如何处理rails3中的404/500错误 [英] how to handle errors like 404 / 500 in rails3
问题描述
嘿,我希望你能帮助我。
Hey, I hope you can help me.
我正在尝试找到一种方法来将用户引导到默认的错误页面404.html和500.html在我的公共文件夹中。
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.
所以当有路由或nomethod错误时,它应该被定向到。
我已经在我的应用程序控制器中尝试了一些东西,但它没有工作。
So when there is a routing or nomethod error it should be directed there to. I already tried some stuff in my application controller but it didnt work.
非常感谢!
推荐答案
Rails在生产模式下运行时自动执行此操作。当您将应用程序上传到实时服务器时,Rails负责处理这些异常,并以正确的标题状态呈现正确的错误页面。如果您想查看这些页面(测试或某些内容),只需直接通过 http:// localhost:3000 / 404.html
Rails does this for you automatically when running in production mode. 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. 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
无论何时在实时服务器上设置Rails应用程序(让我们以Apache为例),您将站点根目录作为 / public
文件夹。然后,每当向该服务器地址发出请求时,Apache首先查找该公用文件夹并尝试提供静态资产(这是
[environment] .rb
)。如果找不到请求的页面,则通过Ruby堆栈转发请求。
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. 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
). If it can't find the requested page, then the request is forwarded through the Ruby stack.
在生产模式下,如果Rails遇到未处理的错误(即开始,抢救),则会将错误全部抛出到堆栈中,然后告诉Apache(在我的例子中,再次)提供一个适当的错误。
When in production mode, if Rails encounters an error that isn't handled (i.e 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.
以下是一些常见错误,您将在开发模式中看到它们在生产中呈现的内容模式:
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)
这篇关于如何处理rails3中的404/500错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!