Rails动态错误页面(404、422、500)显示为空白

编程入门 行业动态 更新时间:2024-10-24 10:26:03
本文介绍了Rails动态错误页面(404、422、500)显示为空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在向应用程序中实现动态错误页面,并且感觉到它正在(现在不存在)模板的 public 文件夹中查找,而不是遵循我设置的路由。

I'm implementing dynamic error pages into an app, and have a feeling it's looking in the public folder for (now non-existent) templates, rather than following the routes I've set up.

在 config / application.rb 中,我在行中添加了 config.exceptions_app = self.routes 来解决这个问题。

In config/application.rb I've added the line config.exceptions_app = self.routes to account for this.

然后我在路由中添加了以下内容:

I've then added the following to my routes:

get "/not-found", :to => "errors#not_found" get "/unacceptable", :to => "errors#unacceptable" get "/internal-error", :to => "errors#internal_error"

错误控制器如下所示:

class ErrorsController < ApplicationController layout 'errors' def not_found render :status => 404 end def unacceptable render :status => 422 end def internal_error render :status => 500 end end

转到 / not -found 按原样显示模板,尽管访问任何不存在的URL(即/ i-dont-exist)都会显示一个空白页。

Going to /not-found shows the template as it should be, though visiting any non-existing URL (i.e. /i-dont-exist) renders an empty page.

我能看到的唯一原因是异常处理需要路由,例如 get / 404,:to =>不过,具有讽刺意味的是, errors#not_found 并未找到/ 404的路由(不,这不仅在起作用:))。

The only reason I could see for this would be that the exception handling needs the routes to be, for example, get "/404", :to => "errors#not_found", though, ironically, it's not finding the route for /404 (and no, that's not just it working :) ).

任何建议,深表感谢。谢谢,史蒂夫。

Any advice, greatly appreciated. Thanks, Steve.

推荐答案

似乎某些设置有误。 在您的路线中尝试以下操作:

It seems some setting is wrong. Try this in your routes:

匹配'/ 404'到:'errors#not_found',通过::all (匹配而不是获取)

您提到在 application.rb 中,您 config.exceptions_app = self.routes ,很好。但是请确保在测试更改之前重新启动服务器。

You mention that in application.rb you have config.exceptions_app = self.routes, that is good. But make sure you are restarting the server before testing your changes.

并确保您的错误视图文件与 ErrorsController 中的操作具有相同的名称。

And make sure your error views files have the same name than the actions in your ErrorsController.

如果您在控制台中遇到任何(haha)错误,可以发布吗?

If you are getting any kind of (haha) error in the console, could you post it?

更多推荐

Rails动态错误页面(404、422、500)显示为空白

本文发布于:2023-10-31 15:39:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546529.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:空白   错误   页面   动态   Rails

发布评论

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

>www.elefans.com

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