如何正确呈现自定义404和500页? [英] How to properly render custom 404 and 500 pages?
问题描述
是否有办法告诉Rails渲染您的自定义错误页面(例如,您在ErrorsController
中编写的页面)?我搜索了很多主题,似乎很有效的一个主题是在您的ApplicationController
中添加类似
Is there are way to tell Rails to render your custom error pages (for example, the ones you write in your ErrorsController
)? I've searched many topics, and the one that seems to kinda work was to add to your ApplicationController
something like
if Rails.env.production?
rescue_from Exception, :with => :render_error
rescue_from ActiveRecord::RecordNotFound, :with => :render_not_found
rescue_from ActionController::UnknownController, :with => :render_not_found
rescue_from ActionController::UnknownAction, :with => :render_not_found
end
,然后按照需要的方式编写方法render_error
和render_not_found
.在我看来,这似乎是一个非常简单的解决方案.而且,这很不好,因为您必须确切地知道所有可能发生的错误.这是一个临时解决方案.
and then you write your methods render_error
and render_not_found
the way you want. This seems to me like a really unelegant solution. Also, it's bad, because you have to know exactly what are all the errors that could happen. It's a temporary solution.
此外,实际上没有简单的方法可以通过这种方式来营救ActionController::RoutingError
.我看到一种方法是添加类似的内容
Also, there is really no easy way to rescue an ActionController::RoutingError
that way. I saw that one way was to add something like
get "*not_found", :to => "errors#not_found"
到routes.rb
.但是,如果您想手动提高ActionController::RoutingError
的位置怎么办?例如,如果不是管理员的人试图通过猜测URL转到管理"控制器.在那种情况下,与引发某种未经授权的访问"错误相比,我更喜欢提出404错误,因为这实际上会告诉该人他猜到了URL.如果您手动将其抬高,它将尝试呈现500页,而我需要404.
to your routes.rb
. But what if you want to raise somewhere an ActionController::RoutingError
manually? For example, if a person who is not an administrator tries to go to "adminy" controllers by guessing the URL. In those situations I prefer raising a 404 more than raising an "unauthorized access" error of some kind, because that would actually tell the person that he guessed the URL. If you raise it manually, it will try to render a 500 page, and I want a 404.
那么有没有办法告诉Rails:在所有情况下,您通常都会渲染404.html
或500.html
,渲染我的自定义404和500页"? (当然,我从public
文件夹中删除了404.html
和500.html
页面.)
So is there a way to tell Rails: "In all cases you would normally render a 404.html
or a 500.html
, render my custom 404 and 500 pages"? (Of course, I deleted the 404.html
and 500.html
pages from the public
folder.)
推荐答案
不幸的是,我所知道的任何方法都不能被覆盖以提供您想要的东西.您可以使用环绕滤镜.您的代码如下所示:
Unfortunately there aren't any methods I know of that can be overridden to provide what you want. You could use an around filter. Your code would look something like this:
class ApplicationController < ActionController::Base
around_filter :catch_exceptions
protected
def catch_exceptions
yield
rescue => exception
if exception.is_a?(ActiveRecord::RecordNotFound)
render_page_not_found
else
render_error
end
end
end
您可以按照自己的意愿处理每种错误.然后,您的#render_page_not_found
和#render_error
方法必须类似于
You can handle each error as you see fit in that method. Then your #render_page_not_found
and #render_error
methods would have to be something like
render :template => 'errors/404'
然后,您需要在app/views/errors/404.html.[haml|erb]
这篇关于如何正确呈现自定义404和500页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!