Rails 4.2.1 上的生产网站
一切都很好,但主页上偶尔会出现奇怪的错误:
Missing template home/index, application/index with {:locale=>[:en], :formats=>["text/html;text/plain"], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in:
[...] /releases/20150619150924/app/views"
[...] shared/bundle/ruby/2.1.0/gems/devise-3.5.1/app/views"
显然 app/views/home/index.html.erb 存在并且大部分时间都工作正常,但似乎偶尔会丢失。不知道这是怎么回事,怎么会只是偶尔发生呢?我从来没有在登台或开发中得到过这个。
请注意,每几百页浏览量才会发生一次。
我在这里遗漏了什么吗?感谢任何指点。
最佳答案
此错误的原因是 header 不正确,您可以通过curl请求简单地重现它:
curl -v -H "Accept: text/html;text/plain" http://your.domain
似乎有人在编写他的机器人时犯了一个错误。这个是正确的:
curl -v -H "Accept: text/html; q=0.2 text/plain" http://your.domain
这个也有效:
curl -v -H "Accept: text/html,text/plain" http://your.domain
RFC:http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
您可以通过中间件修复它:
# lib/fix_accept_header.rb
class FixAcceptHeader
def initialize(app)
@app = app
end
def call(env)
if env["HTTP_ACCEPT"] =~ %r(text/html;\s*text/plain)
env["HTTP_ACCEPT"] = "text/html, text/plain"
end
@app.call(env)
end
end
# config/application.rb
require File.expand_path('../../lib/fix_accept_header', __FILE__)
#...
class Application < Rails::Application
#...
config.middleware.use FixAcceptHeader
end
关于ruby-on-rails - Rails 缺少模板错误,只是间歇性的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30952782/