gpt4 book ai didi

ruby-on-rails - Rails 4 - 缺少模板/缺少部分错误,仅在生产中

转载 作者:行者123 更新时间:2023-12-03 17:33:30 25 4
gpt4 key购买 nike

我们刚刚使用 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. '

Controller :

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(缺少部分行程/_test with {:locale=>[:en], :formats=>[:url_encoded_form], :variants=>[], :handlers=>[:erb, :builder , :raw, :ruby, :coffee, :jbuilder]}。

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

生产:

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

发展:

format requested: */*
format url_encodeded: false

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

  • 更改 CoffeeScript 中每个 AJAX 调用的内容类型?
  • 向 Controller 添加一个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:

localhost 和 production 的请求 header 相同,内容类型为 application/x-www-form-urlencoded,即使 format.url_encoded_form? 返回错误。

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])

关于ruby-on-rails - Rails 4 - 缺少模板/缺少部分错误,仅在生产中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38958369/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com