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

时间:2022-11-29 16:18:33

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. I already tried some stuff in my application controller but it didnt work.

因此,当存在路由或nomethod错误时,它应该指向那里。我已经在我的应用程序控制器中尝试了一些东西,但它没有用。

Many thanks!!

3 个解决方案

#1


19  

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会自动为您执行此操作。当您将应用程序上传到实时服务器时,Rails负责处理这些异常并使用正确的标头状态呈现正确的错误页面。如果您正在尝试查看这些页面的外观(用于测试或其他内容),只需通过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. 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为例),您将站点root作为应用程序中的/ public文件夹。然后,每当向该服务器地址发出请求时,Apache首先查找该公用文件夹并尝试提供静态资产(这是[environment] .rb中的可配置选项)。如果找不到请求的页面,则通过Ruby堆栈转发请求。

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.

在生产模式下,如果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)

#2


2  

Happens automatically if run in production mode - no need that you do that manually.

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

#3


2  

Have a look at this post to redirect all requests causing a Routing Error.

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

#1


19  

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会自动为您执行此操作。当您将应用程序上传到实时服务器时,Rails负责处理这些异常并使用正确的标头状态呈现正确的错误页面。如果您正在尝试查看这些页面的外观(用于测试或其他内容),只需通过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. 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为例),您将站点root作为应用程序中的/ public文件夹。然后,每当向该服务器地址发出请求时,Apache首先查找该公用文件夹并尝试提供静态资产(这是[environment] .rb中的可配置选项)。如果找不到请求的页面,则通过Ruby堆栈转发请求。

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.

在生产模式下,如果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)

#2


2  

Happens automatically if run in production mode - no need that you do that manually.

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

#3


2  

Have a look at this post to redirect all requests causing a Routing Error.

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