如何正确渲染自定义404和500页面?
有没有办法告诉 Rails 呈现您的自定义错误页面(例如,您在 ErrorsController
中编写的页面)?我搜索了很多主题,似乎有点工作的一个是向您的 ApplicationController
添加类似的内容
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< /code> 你想要的方式。在我看来,这是一个非常不优雅的解决方案。而且,这也很糟糕,因为您必须确切地知道可能发生的所有错误是什么。这是一个临时解决方案。
另外,确实没有简单的方法可以通过这种方式挽救 ActionController::RoutingError
。我发现一种方法是
get "*not_found", :to => "errors#not_found"
向您的 routes.rb
添加类似的内容。但是,如果您想在某个地方手动引发 ActionController::RoutingError
该怎么办?例如,如果非管理员尝试通过猜测 URL 访问“管理”控制器。在这些情况下,我更喜欢引发 404,而不是引发某种“未经授权的访问”错误,因为这实际上会告诉人们他猜到了 URL。如果你手动提升它,它会尝试渲染一个 500 页面,而我想要一个 404。
那么有没有办法告诉 Rails:“在所有情况下,你通常会渲染一个 404.html
或a 500.html
,渲染我的自定义 404 和 500 页面”? (当然,我从 public
文件夹中删除了 404.html
和 500.html
页面。)
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
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.
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"
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.
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.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,据我所知,没有任何方法可以被重写来提供您想要的东西。您可以使用周围过滤器。您的代码将如下所示:
您可以按照您认为适合该方法的方式处理每个错误。那么您的
#render_page_not_found
和#render_error
方法必须类似于您需要在
app/views/errors/404.html 处有一个文件。 [haml|erb]
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:
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 likeYou would then need to have a file at
app/views/errors/404.html.[haml|erb]