Rails 4 - MissingTemplate/Missing 部分错误,仅在生产中

Rails 4 - MissingTemplate/Missing Partial Error, Only in Production

我们刚刚使用 SSL 证书保护我们的应用程序。事实证明,迁移到 HTTPS 比我们想象的要复杂,我们目前正在对由此产生的一些错误进行分类。

我们在 CoffeeScript 中有一个 AJAX 调用,其中 rails 通过呈现部分的 html 进行响应。这在开发中非常有效。

咖啡脚本:

coffee_method: (pos, uid) =>

  $.ajax '/contoller/test/',
    type: 'POST'
    data:
      pos: pos
      uid: uid
    success: (data) ->
      $('#result-div').html(data) #Populates side menu with _next_destination_menu content
    error: ->
      alert 'How embarassing! Something went wrong - please try your search again. '

控制器:

def test
  ... #do some stuff

  puts "format requested: #{request.format}"
  puts "format url_encodeded: #{request.format.url_encoded_form?}"

  render partial: 'trips/_test' #app/views/trips/_test.html.erb

end

然而,在生产中,我们得到以下错误: ActionView::MissingTemplate (Missing partial trips/_test with {:locale=>[:en], :formats=>[:url_encoded_form], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}.

经过一些挖掘,我发现生产中的请求是一种不同的格式。使用控制器中的 puts 行进行调试,结果如下:

生产:

format requested: application/x-www-form-urlencoded
format url_encodeded: true

发展:

format requested: */*
format url_encodeded: false

处理此问题的最佳方法是什么。我是否:

后者似乎是在重复代码,因为我想呈现相同的部分,而不管请求的格式如何。我试过 format.all {...} 但遇到了同样的问题。感谢任何最佳实践!

更新:

直接指定响应类型给我相同的缺少模板错误:

respond_to do |format|
  format.html  {render partial: 'trips/test'}
  format.json  {render partial: 'trips/test' }
  format.url_encoded_form {render partial: 'trips/test'}
end

更新二:

localhost 和 production 的请求 headers 相同,内容类型为 application/x-www-form-urlencoded,即使 format.url_encoded_form? returns false。

Accept:*/*
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4
Connection:keep-alive
Content-Length:37
Content-Type:application/x-www-form-urlencoded; charset=UTF-8

您可以尝试完全按照要求传递格式、语言环境和处理程序:

render(
     partial: 'trips/test',
     formats: [:html, :js, :json, :url_encoded_form],
     locale: [:en],
     handlers: [:erb, :builder, :raw, :ruby, :coffee, :jbuilder])