由于浏览器发送的格式不正确,导致Rails出现MissingTemplate错误

MissingTemplate error in Rails due to incorrect formats sent by browsers

本文关键字:导致 Rails 出现 错误 MissingTemplate 不正确 格式 浏览器      更新时间:2023-09-26

我遇到了一个问题,偶尔浏览器在发出请求时无法发送正确的格式列表,结果抛出MissingTemplate错误。我有只响应JS的动作:例如,类似于:

blah_controller.rb:

def BlahController < ActionController::Base
  respond_to :js, :only => :my_action
  def my_action
    # stuff
  end
end

my_action.js.erb:

doSomething();

绝大多数情况下,jQuery发出ajax请求,然后执行生成的JavaScript——调用doSomething()。但是偶尔,浏览器会发送一个不包含JS的格式列表,因此会抛出一个ActionView::MissingTemplate错误:

ActionView::MissingTemplate: Missing template blah/my_action with {:formats=>[:html], :handlers=>[:rjs, :rhtml, :erb, :rxml, :builder], :locale=>[:en, :en]} in view paths ...

这种情况发生在不同的浏览器上,所以我不能将其归因于某个模糊的浏览器,但我无法复制它

如果您无法重现该问题,并且它似乎只发生在您的生产环境中,则可能有些机器人错误地请求您的页面。您在浏览器中看到的差异可能来自欺骗的用户代理。

我会根据项目蜜罐

检查失败请求的iphttp://www.projecthoneypot.org/