Rails:是在没有任何设置的情况下呈现的静态错误页面(500.html、404.html)

Rails: are static error pages (500.html, 404.html) rendered without any setup

我正在调试一个应用程序,我看到一个设置为

config.consider_all_requests_local = false

在 config/environments/staging.rb 中,500.html 文件在 public 目录中。

那么如果header中的status是500,rails会自动显示500.html吗?或者我是否需要进行额外的设置以便在发生异常时显示 500.html

我正在使用 rails 3.1,如有任何帮助,我们将不胜感激

是的,它会自动加载静态 html 文件 public/500.html 和 public/404.html.

作为静态文件,这些文件不会通过通常的 Rails 管道。

对于其他消息代码,或者如果您想使用 Ruby 代码 and/or 数据库访问等高级内容自定义错误消息,您需要执行以下操作:

  • 添加必要的路线;
  • 创建一个 ErrorsController 及其对应的操作和视图。

# config/routes.rb
get "500", to "errors#error_500", code: "500"

# app/controllers/ErrorsController.rb
class ErrorsController < ApplicationController
  def error_500
  end
end

是的。

14.1 The Default 500 and 404 Templates

By default a production application will render either a 404 or a 500 error message. These messages are contained in static HTML files in the public folder, in 404.html and 500.html respectively. You can customize these files to add some extra information and layout, but remember that they are static; i.e. you can't use RHTML or layouts in them, just plain HTML.

Action Controller Overview