ruby-on-rails - ActionText 的非图像附件问题
问题描述
我正在尝试处理上传不是带有操作文本的图像的文件的问题,并使它们对查看帖子的用户起作用。例如,如果在创建某些内容时上传了 PDF,用户应该能够下载。
现在我的图片上传和显示都很好,但是如果我上传了 PDF,它会显示文件名和大小:
Bla.pdf 635 KB。看起来这是不可点击的:
下面的html是:
<action-text-attachment sgid=”BAh7CEkiCGdpZAY6BkVUSSI4Z2lkOi8vdGFhYWxrLWVkZ2UvQWN0aXZlU3RvcmFnZTo6QmxvYi82OD9leHBpcmVzX2luBjsAVEkiDHB1cnBvc2UGOwBUSSIPYXR0YWNoYWJsZQY7AFRJIg9leHBpcmVzX2F0BjsAVDA= — 9fc1638e6a6bca562cbff92f1627dfcf9e74f198" content-type=”application/pdf” url="http://localhost:3000/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBTUT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--5da8c8c82a642f3ed3f0c158a2560a7a41b974c7/Bla.pdf" filename=”Bla.pdf” filesize=”649993"><figure class=”attachment attachment — file attachment — pdf”>
<figcaption class=”attachment__caption”>
<span class=”attachment__name”>Bla.pdf</span>
<span class=”attachment__size”>635 KB</span>
</figcaption>
</figure></action-text-attachment>
有什么想法可以让我下载吗?
谢谢
解决方案
你需要改变
app/views/active_storage/blobs/_blob.html.erb
像这个
<figure class="attachment attachment--<%= blob.representable? ? "preview" : "file" %> attachment--<%= blob.filename.extension %>">
<% if blob.representable? %>
<%= image_tag blob.representation(resize_to_limit: local_assigns[:in_gallery] ? [ 800, 600 ] : [ 1024, 768 ]) %>
<figcaption class="attachment__caption">
<% if caption = blob.try(:caption) %>
<%= caption %>
<% else %>
<span class="attachment__name"><%= blob.filename %></span>
<span class="attachment__size"><%= number_to_human_size blob.byte_size %></span>
<% end %>
</figcaption>
<% else %>
<%= link_to blob.filename, rails_blob_path(blob) %>
<% end %>
</figure>
推荐阅读
- python - 删除数组中每个元素的第一个数字
- awk - 将换行符转换为 '\n'
- firebase - Firestore:在控制台中显示集合文档
- python - 是否有使包符合 PEP-561 的最佳实践?
- hex - 未知 CRC 方法
- visual-studio-code - 如何知道在vscode中选择了什么建议项
- linux - 在 Bash 中激活 Venv 而不更改子目录
- python - python中的Socket模块学习起来非常烦人
- c - 这是未定义的行为(字符串修改)
- python-3.x - Python 图像保存错误 - 从 e ValueError 引发 ValueError("unknown file extension: {}".format(ext)): unknown file extension: