我目前正在开发一个 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)
应该渲染附件文件,但它并不如下图所示
但是,如果我将所有内容都注释掉并且只有以下行。 <%= cl_image_tag(blob.key, format: :jpg )%>
。图像在没有 _blob.html.erb
的 HTML 结构的情况下渲染。 。
为什么是_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
并重新启动应用程序后,图像就会显示。
注意:以上链接指向 rails 的 master
分支。您可能需要根据您使用的 Rails 版本选择正确的标签。
希望这有帮助。
关于ruby-on-rails - rails 6 : ActionText attached image not rendering in _blob partial template,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60894740/