Rails 2.3 中的 404 自定义错误页面
每当出现错误时,我都会重写 render_Optional_error_file 以呈现自定义页面。如果应用程序中出现错误,这会非常有效,它会毫无问题地呈现“shared/error.erb”。
我的应用程序控制器有一些 before_filters,它们负责设置页面的配色方案、定义菜单项和验证用户身份。当出现应用程序错误时,它们也会运行,这是所希望的。
但是,当呈现文件的 404 页面时,这些过滤器都不会运行,因此我得到一个没有菜单的黑色页面。有什么办法可以触发它们运行吗?我有什么理由不应该这样做吗?
I'm overriding render_optional_error_file to render a custom page whenever there's an error. This works great if there's an error within the application, it renders "shared/error.erb" without problem.
My application controller has a few before_filters which are responsible for setting the colour scheme of the page, defining the menu items, and authenticating users. These also run when there's an application error, which is desired.
However when a 404 page for a file is rendered, none of these filters run, so I get a black page with no menu. Is there a way I can trigger these to run? And are there any reasons why I shouldn't do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当 Rails 遇到丢失的文件时,它会在应用程序控制器上运行状态为 404 的 render_Optional_error_file(404),但会跳过所有过滤器,大概是因为已经发生了错误。
我向应用程序控制器添加了一个名为 run_filters 的方法,然后从 render_Optional_error_file 调用该方法:
您还可以通过在应用程序控制器中包含以下内容来在开发服务器上测试此行为:
When Rails encounters a missing file, it runs render_optional_error_file(404) with status 404 on the application controller, but skips all filters, presumably since an error has already occurred.
I added a method called run_filters to my application controller and then call that from render_optional_error_file:
You can also test this behaviour on your development server by including the following in your application controller:
那么,您可能需要考虑为错误页面创建特殊布局。毕竟,将错误页面与正常页面清晰地区分开来是一种常见的做法。
但我不明白为什么应用程序控制器中定义的之前过滤器不触发。它们确实应该在您的操作中发生任何错误之前触发。
您能为我们提供您的应用程序控制器中的一些代码吗?
Well, you may want to consider to create a special layout for your error pages. After all, it's a common practice to make error pages clearly distinguishable from normal ones.
But I don't understand why your before filters defined in application controller don't trigger. They really should trigger before any error happens in your actions.
Could you provide us with some code from your application controller?