Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionView::MissingTemplate Error, Only When Visited By A Bot?

I have an action that serves my homepage. It works fine when visited normally (ie by a user in a web browser), but when visited by specific web crawlers, it throws the following error:

 A ActionView::MissingTemplate occurred in tags#promoted:

 Missing template tags/promoted with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>["text/*"], :locale=>[:en, :en]} in view paths "/Apps/accounts/app/views", "/usr/local/rvm/gems/ruby-1.9.2-p180@accounts/gems/devise-1.3.0/app/views"
 actionpack (3.0.4) lib/action_view/paths.rb:15:in `find'

It appears the bots are trying to fetch the text/* format, which there is no template for, which makes sense, so I tried to do the following in my action:

  def promoted
   request.format = :html #force html to avoid causing missing template errors
   # more action stuff....
  end

In essence, I am trying to force the request's format to html so it serves the html template.

Yet every time these set of bots request this page, the missing template error occurs.

It's not that big of deal, but ideally I'd like to resolve this error, if only so I stop getting these error emails from my app.

Is the only way to make a file called my_action.text.erb and put some gibberish in it? Or can I solve this more elegantly?

like image 512
Alex Avatar asked May 10 '11 03:05

Alex


1 Answers

I've been seeing these as well. You could use some middleware to rewrite these requests:

class Bot
  def initialize(app)
    @app = app
  end

  def call(env)
    h = env["HTTP_ACCEPT"]
    env["HTTP_ACCEPT"] = "text/html" if h == "text/*"
    @app.call(env)
  end
end

I forked a gem for killing off some MS Office Discovery Requests, and it seemed to make sense to add this middleware into it.

https://github.com/jwigal/rack-options-request

like image 195
Jeff Wigal Avatar answered Nov 15 '22 19:11

Jeff Wigal