【问题标题】:Strange MissingTemplate exception with :formats=>[:jpeg, "image/pjpeg", :png, :gif]奇怪的 MissingTemplate 异常与 :formats=>[:jpeg, "image/pjpeg", :png, :gif]
【发布时间】:2013-09-19 11:56:19
【问题描述】:
在生产中,我们经常会遇到以下异常:
一个 ActionView::MissingTemplate 发生在构造#show:
缺少模板构造/显示,应用程序/显示与 {:locale=3D>[:=
ru], :formats=3D>[:jpeg, "图像/pjpeg", :png, :gif], :handlers=3D>[:erb, :b=
uilder, :coffee, :jbuilder, :haml]}
令我困惑的是格式哈希,它请求一些图像(:jpeg、“image/pjpeg”、:png、:gif)。我们的应用程序中没有注册自定义 MIME 类型,据我所知,没有对应的 Rails 默认 MIME 类型。
那么问题来了:什么样的请求会生成这种格式的哈希?
【问题讨论】:
标签:
ruby-on-rails
exception
mime-types
【解决方案1】:
我也遇到了同样的错误。我注意到这是来自试图获取自定义格式的“YandexImage”搜索引擎。在我的控制器上,action 是空的,因为它是一个静态的 *.html.erb 页面。这里有更多信息。
* DOCUMENT_ROOT : /srv/www/apps/mysite/current/public
* HTTP_ACCEPT : image/jpeg, image/pjpeg, image/png, image/gif
* HTTP_CONNECTION : Keep-Alive
* HTTP_FROM : support@search.yandex.ru
* HTTP_HOST : mysite.com
* HTTP_USER_AGENT : Mozilla/5.0 (compatible; YandexImages/3.0; +http://yandex.com/bots)
* ORIGINAL_FULLPATH : /
解决此问题的两种方法:
编辑 public/robots.txt 以阻止 YandexImage。在http://yandex.com/bots查看更多信息
User-agent: YandexImage
Disallow: /
或将以下代码添加到您的操作中,它只会处理 html,否则会引发未找到的页面
respond_to do |format|
format.html
format.any { raise ActionController::RoutingError.new('Not Found') }
end