Rails missing template error, only intermittently

761 views Asked by At

Production website on Rails 4.2.1

Everything fine, but occasionally get strange error on the home page:

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"

Obviously app/views/home/index.html.erb exists and works fine most of time, but it seems to miss once in a while. Not sure what is going on here, how can this happen only occasionally? I never get this on staging or dev.

Please note only happens once every few hundred page views.

Am I missing something here? Be grateful for any pointers.

1

There are 1 answers

1
ole On BEST ANSWER

The reason of this error is the incorrect header, you can simply reproduce it via the curl request:

curl -v -H "Accept: text/html;text/plain" http://your.domain

Seems someone made a mistake when wrote his bot. This one is correct:

curl -v -H "Accept: text/html; q=0.2 text/plain" http://your.domain

This one is valid too:

curl -v -H "Accept: text/html,text/plain" http://your.domain

RFC: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1

You can fix it through the middleware:

# 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