开发者

Rails 3 and Strange Accept Headers

开发者 https://www.devze.com 2023-02-08 11:36 出处:网络
My Rails 3 site is getting hit by crawlers with strange accept headers, trigger exceptions as like ActionView::MissingTemplate occurred in home#show

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 bein开发者_Python百科g 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.


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


Because SO prevents adding comments until I have 50 reputation, I must submit a new answer to reply to Ryan Bigg's question in the comments.

not request.format == "html" is more or less the same thing as request.format != "html". and, or and not are logically identical to &&, || and ! - however, they have much lower precedence. So, in this example, the == operator evaluates before the not operator, such that it produces the same result as using !=.

0

精彩评论

暂无评论...
验证码 换一张
取 消