Rails动态错误页面(404、422、500)显示为空白 [英] Rails dynamic error pages (404, 422, 500) showing as blank
问题描述
我正在向应用程序中实现动态错误页面,并且感觉到它正在(现在不存在)模板的 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)显示为空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!