【问题标题】:Rails 6: ActionText attached image not rendering in _blob partial templateRails 6:ActionText 附加图像未在 _blob 部分模板中呈现
【发布时间】:2020-07-08 16:59:42
【问题描述】:

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

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

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

意见/问题/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) 应该正在渲染附件文件,但它没有如下图所示

但是,如果我要评论所有内容并且只有以下行。 &lt;%= cl_image_tag(blob.key, format: :jpg )%&gt;。该图像是在没有来自_blob.html.erb 的 HTML 结构的情况下呈现的。

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

【问题讨论】:

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


    【解决方案1】:

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

    如果您查看representable? 的源代码,它又调用variable?,您可以看到content types 它检查不包括image/heic。您可以配置为将其包含在文档中的here

    config.active_storage.variable_content_types 接受一个字符串数组,指示 Active Storage 可以通过 ImageMagick 转换的内容类型。默认为%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 版本选择正确的标签。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-20
      • 1970-01-01
      • 2020-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-29
      • 1970-01-01
      相关资源
      最近更新 更多