Rails 3和Strange Accept Headers
我的Rails 3网站受到具有奇怪接受标头的爬虫的攻击,触发exception等
ActionView::MissingTemplate occurred in home#show
以下是一些导致问题的接受标头
text/* application/jxw */*;q=0.1
在这些情况下,这被解释为请求的格式,因此导致丢失模板错误。 我真的不在乎我回到这些爬虫,但只是想避免exception。
你可以在应用程序控制器中从这样的exception中解救出来并改为呈现HTML模板:
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
因为在我有50个声望之前,SO会阻止添加评论,所以我必须在评论中提交一个新答案来回复Ryan Bigg的问题。
not request.format == "html"
或多或少与request.format != "html"
。 and
, or
在逻辑上与&&
, ||
相同 而且!
– 但是,它们的优先级要低得多。 因此,在此示例中, ==
运算符在not
运算符之前求值,使得它产生与使用!=
相同的结果。