Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 and Strange Accept Headers

My Rails 3 site is getting hit by crawlers with strange accept headers, trigger exceptions as like

ActionView::MissingTemplate occurred in home#show

Here are some of the accept headers causing issues

text/*
application/jxw
*/*;q=0.1

In these cases, this is being interpreted as the format for the request, and as such, causing the missing template error. I don't really care what I return to these crawlers, but just want to avoid the exceptions.

like image 611
Paul McMahon Avatar asked Feb 05 '11 04:02

Paul McMahon


1 Answers

You could rescue from exception like this in your application controller and render the HTML template instead:

class ApplicationController
  rescue_from ActionView::MissingTemplate, :with => :render_html

  def render_html
    if not request.format == "html" and Rails.env.production?
      render :format => "html"
    else
      raise ActionView::MissingTemplate
    end
  end
end
like image 127
Max Schulze Avatar answered Oct 26 '22 03:10

Max Schulze