Rails 4 - 生产环境下出现的MissingTemplate/Missing Partial错误

3

我们刚刚通过SSL证书保护了我们的应用程序。迁移到HTTPS比我们想象的要复杂,目前我们正在解决一些相关的缺陷。

我们在CoffeeScript中使用了一个AJAX调用,其中Rails通过呈现部分HTML来响应。在开发中,这个工作得非常完美。

CoffeeScript代码:

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 (缺少部分 trips/_test,其中 {: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

如何处理这个问题最好的方法是什么?我应该:

  • 更改CoffeeScript中每个AJAX调用的内容类型?
  • 在控制器中添加respond_to...format.url_encoded_form {render partial: trips/test}

后者似乎会重复代码,因为我想呈现相同的局部视图,无论请求以哪种格式发出。我尝试了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

更新2:

本地主机和生产环境的请求头相同,内容类型为application/x-www-form-urlencoded,即使format.url_encoded_form?返回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

你是否使用了代理(例如nginx)来更改请求? - Nick Tomlin
@NickTomlin - 我不认为我们正在使用代理,但我们正在使用AWS CloudFront进行Web分发。 - CHawk
1个回答

4
您可以尝试按照要求精确传递格式、区域和处理程序:
render(
     partial: 'trips/test',
     formats: [:html, :js, :json, :url_encoded_form],
     locale: [:en],
     handlers: [:erb, :builder, :raw, :ruby, :coffee, :jbuilder])

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接