【问题标题】:Using #send_data to send image from database broke with Rails 4.2使用 #send_data 从数据库发送图像与 Rails 4.2 冲突
【发布时间】:2021-01-26 06:57:40
【问题描述】:

我有一个控制器可以渲染在 Rails 3.2、4.0 和 4.1 中运行的图像。当我升级到 4.2 时,我开始遇到问题。这是我的代码:

class BookCoversController < ApplicationController
  def show
    @image = BookCover.for_book_id(params[:book_id])
    
    # We use only an ETag here so that clients will have to check that the image is still valid.
    if stale?(:etag => @image.cache_key)
      send_data(@image.image, :type => @image.content_type, :filename => "book_cover_#{@image.book_id}", :disposition => 'inline')
    end
  end
end

当我转到 4.2 时,我收到消息 Couldn't find template for digesting: book_covers/show 并且图像没有呈现。检查文档中的#stale? 发现并将template: false 添加到#stale? 调用中。这使得消息从日志中消失,但图像仍然没有显示。我可以验证代码是否正在调用#send_data,但浏览器没有接收到图像。我也尝试添加render: nothing,但Rendered text templateSent data 消息都从日志中消失了,所以我认为这没有任何帮助。

更多信息:以下是图像正确显示的生产(Heroku,Rails 3.2)的响应标头列表,然后是我的开发服务器(本地 Mac OS X 10.14)上的响应标头,其中图像不显示.

还有一条信息。在生产中,响应正文开始如下:

在开发过程中,响应正文如下所示:

【问题讨论】:

  • 内容主体是什么?你的标题看起来大部分是正确的(虽然我希望有一个Content-Length 标题)。
  • @danthedaniel 身体看起来完全不同,但我不确定这意味着什么。
  • 从开发数据库和生产数据库读取时,两者是否显示不同的内容?
  • 是的,但这不是因为生产和开发,而是因为生产是Rails 3.2,而开发是4.2。当 dev 是 4.1、4.0 和 3.2 时,它工作得很好。
  • 我刚刚找到了一种解决方法,但我不知道它为什么有效。解决方法是在图像字节字符串上调用 PG::Connection.unescape_bytea()。这似乎在 PG 10 中发生了变化。我的生产数据库是 9.6,它不需要该调用,但我的 10.0 数据库需要(也可能发生在我为迁移到 PG 10 而“pgdump”本地数据库时。

标签: ruby-on-rails image ruby-on-rails-4 caching streaming


【解决方案1】:

PG

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-27
    • 2021-10-31
    • 2021-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-15
    • 1970-01-01
    相关资源
    最近更新 更多