Rails 3 和奇怪的接受头 [英] Rails 3 and Strange Accept Headers

查看:19
本文介绍了Rails 3 和奇怪的接受头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Rails 3 站点被带有奇怪接受标头的爬虫攻击,触发异常

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.

推荐答案

你可以在你的应用程序控制器中从这样的异常中拯救出来并渲染 HTML 模板:

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

这篇关于Rails 3 和奇怪的接受头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆