【发布时间】:2012-03-10 02:49:11
【问题描述】:
从 Rails 3.0 开始,我不时收到这样的异常通知:
ActionView::MissingTemplate: Missing template [...] with {:locale=>[:en],
:formats=>[:text], :handlers=>[:erb, :builder, :haml]}. Searched in: * [...]
例如,像http://example.com/some/path/robots.txt 这样的任意手写 URL 会引发错误。不好玩。
我很久以前就在这个ticket中报告了这个问题,并且一直在使用这里提到的补丁,但问题仍然存在。
在这篇博文中提出了修复建议,
http://trevorturk.wordpress.com/2011/12/09/handling-actionviewmissingtemplate-exceptions/
要使用这个:
respond_to do |format|
format.js
end
但这对我来说感觉不对,因为我对使用多种格式重载动作不感兴趣。在我的应用中,HTML 和 JSON API 有单独的 URL,所以简单的 render 就足够了。
我应该直接吞下rescue_from ActionView::MissingTemplate 的异常并自己返回 406 吗?
有没有更好的方法来处理这种情况?
或者我可以这样问 - 首先,在生产中提出这种异常是否有任何实际用途?
【问题讨论】:
标签: ruby-on-rails templates exception