生产中的 rails 应用出现奇怪的 "Missing template" 异常

Weird "Missing template" exception in rails app on production

我在 rails 应用程序中设置了异常通知程序。所以今天我收到第二个通知,index 模板丢失:

An ActionView::MissingTemplate occurred in products#index:

Missing template products/index, application/index with {:locale=>[:en, :de], :formats=>["text/html;text/plain"], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :slim, :arb, :jbuilder]}. Searched in:
* "/home/releases/20160518143810/app/views"


-------------------------------
Request:
-------------------------------

* URL : http://example.com/shop
* HTTP Method: GET
* IP address : xx.xx.xx.xx
* Parameters : {"controller"=>"products", "action"=>"index"}
* Timestamp : 2016-06-07 08:19:13 +0200
* Server : vintage-shop.ch
* Rails root : /home/releases/20160518143810
* Process: 15714

我知道一个事实,产品的索引模板在服务器上并在 VCS 中签入,应用程序已经 运行 一个月了。所以我真的很想知道,为什么会发生这种情况:

$ ls /home/releases/20160518143810/app/views/products/index.html.slim 

# -> /home/releases/20160518143810/app/views/products/index.html.slim

这种异常怎么可能?

更新:我查找了 IP 地址,它不是来自我希望我的客户来自的区域。所以我想知道这是否是某种攻击,但问题仍然是如何触发异常,即使模板存在。

这可能是一次攻击尝试或(我猜更有可能)一些随机噪音。问题似乎出在 Rails 从请求 header 中解析出来的 格式 中。通常,您应该在异常消息中看到类似以下内容的内容::formats=>[:html, :text, :js, :css, :ics]

格式通常在 rails 中使用请求 URI 确定(通常点 '.' 之后的所有内容都被视为格式规范)或使用 Accept header .在你的情况下,我猜是 header (你应该能够在异常通知中看到 header )。 Accept header 应该包含 comma-separated 内容类型,而不是 分号-分隔,所以我猜这是异常的实际来源。

如果您想了解有关 Rails 格式分辨率的更多详细信息,请查看这个 insightful blog post