ruby-on-rails - rails 6 : ActionText attached image not rendering in _blob partial template

标签 ruby-on-rails rails-activestorage ruby-on-rails-6 actiontext

我目前正在开发一个 Rails 6 应用程序,其中使用 cloduinary 来托管图像。我有一个模型 rich_text_content .

class Question < ApplicationRecord
  belongs_to :user
  has_many :comments, as: :commentable
  has_rich_text :content
end

当我从富文本编辑器添加图像并尝试渲染该图像时,浏览器不会渲染附加图像。

views/questions/show.html.erb

<div class="border-t-2 border-gray-600 text-2xl m-auto w-4/5">
    <div class="h-56 my-8">
      <%= @question.content %>
    </div>
  </div>

show.html.erb模板仅显示文本内容,而不显示 @question.content 中的附件图像所以当有 embeds_blob _blob.html.erb未显示图像。

<figure class="attachment attachment--<%= blob.representable? ? "preview" : "file" %> attachment--<%= blob.filename.extension %>">
  <% if blob.representable? %>
      <div class="max-w-xl mx-auto rounded overflow-hidden shadow-lg">
        <%= cl_image_tag(blob.key, format: :jpg )%>
        <div class="px-6 py-4">
          <figcaption class="attachment__caption">
            <% if caption = blob.try(:caption) %>
              <%= caption %>
            <% else %>
              <div class="inline-block bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2">
                <span class="attachment__name"><%= blob.filename %></span>
                <span class="attachment__size"><%= number_to_human_size blob.byte_size %></span>
              </div>
            <% end %>
          </figcaption>
        </div>
      </div>
  <% end %>
</figure>

cl_image_tag(blob.key, format: :jpg)应该渲染附件文件,但它并不如下图所示 enter image description here

但是,如果我将所有内容都注释掉并且只有以下行。 <%= cl_image_tag(blob.key, format: :jpg )%> 。图像在没有 _blob.html.erb 的 HTML 结构的情况下渲染。 。 enter image description here

为什么是_blob.html.erb不渲染来自 Cloudinary 的附加图像?

最佳答案

您可能已经知道,部分中的 blob.representable? 方法调用返回 false

如果你看representable?的源代码这又调用 variable? ,您可以看到content types它检查不包含image/heic。您可以配置为包含它,如文档 here 所示。 .

config.active_storage.variable_content_types accepts an array of strings indicating the content types that Active Storage can transform through ImageMagick. The default is %w(image/png image/gif image/jpg image/jpeg image/pjpeg image/tiff image/bmp image/vnd.adobe.photoshop image/vnd.microsoft.icon image/webp)

包含 image/heic 并重新启动应用程序后,图像就会显示。

注意:以上链接指向 railsmaster 分支。您可能需要根据您使用的 Rails 版本选择正确的标签。

希望这有帮助。

关于ruby-on-rails - rails 6 : ActionText attached image not rendering in _blob partial template,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60894740/

相关文章:

mysql - 如何将我的插入物分成更小的部分?

ruby-on-rails - ruby 中的树结构与父子数组格式没有 gem ?

ruby-on-rails - 在 Ruby on Rails 中使用网络摄像头

ruby-on-rails - Postgresql 上带有 UUID 的 Rails 5.2 ActiveStorage

jquery 事件未使用 webpacker 和 Coffeescript 触发

ruby-on-rails - 将值数组发送到 ruby​​ 中的 sql 查询?

ruby-on-rails - 如何在 Controller RSpec 中测试设计,错误:未定义方法 `assign' for #<RSpec::Core::ExampleGroup::Nested_1:0x0000010597f4b8>

ruby-on-rails - 在 Active Record 查询中包含 Active Storage 附件

ruby-on-rails - 如何在 Rails 6 中使用 Webpacker 跨多个客户端 JavaScript 文件共享变量和函数?

ruby-on-rails - 在 Rails 6 中按计数排序