Rails 3 and Strange Accept Headers

有些话、适合烂在心里 提交于 2019-12-30 03:18:08

问题


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.


回答1:


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



回答2:


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 !=.



来源:https://stackoverflow.com/questions/4905229/rails-3-and-strange-accept-headers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!