如何正确呈现自定义404和500页?

编程入门 行业动态 更新时间:2024-10-10 23:15:55
本文介绍了如何正确呈现自定义404和500页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否有办法告诉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页?

本文发布于:2023-11-02 06:02:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1551563.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   如何正确

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!