Rails缺少模板错误,只是间歇性地

时间:2015-06-20 10:10:53

标签: ruby-on-rails erb

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存在并且大部分时间都能正常工作,但似乎偶尔会错过。不确定这里发生了什么,这怎么会偶尔发生?我从未在演出或开发时得到这个。

请注意,每隔几百页就会发生一次。

我在这里遗漏了什么吗?感谢任何指示。

1 个答案:

答案 0 :(得分:3)

此错误的原因是标题不正确,您可以通过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