ActionText 的非图像附件问题

Jos*_*ers 3 ruby-on-rails file ruby-on-rails-6 actiontext

我正在尝试解决上传非带有操作文本的图像的文件的问题,并使它们对查看帖子的用户有用。例如,如果在创建某些内容时上传了 PDF,用户应该能够下载。

\n\n

现在我的图像上传和显示都很好,但如果我上传了 PDF,它会显示文件名和大小:

\n\n

Bla.pdf 635 KB。看起来这是不可点击的:

\n\n

PDF 文件无法下载

\n\n

下面的 html 是:

\n\n
<action-text-attachment sgid=\xe2\x80\x9dBAh7CEkiCGdpZAY6BkVUSSI4Z2lkOi8vdGFhYWxrLWVkZ2UvQWN0aXZlU3RvcmFnZTo6QmxvYi82OD9leHBpcmVzX2luBjsAVEkiDHB1cnBvc2UGOwBUSSIPYXR0YWNoYWJsZQY7AFRJIg9leHBpcmVzX2F0BjsAVDA= \xe2\x80\x94 9fc1638e6a6bca562cbff92f1627dfcf9e74f198" content-type=\xe2\x80\x9dapplication/pdf\xe2\x80\x9d url="http://localhost:3000/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBTUT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--5da8c8c82a642f3ed3f0c158a2560a7a41b974c7/Bla.pdf" filename=\xe2\x80\x9dBla.pdf\xe2\x80\x9d filesize=\xe2\x80\x9d649993"><figure class=\xe2\x80\x9dattachment attachment \xe2\x80\x94 file attachment \xe2\x80\x94 pdf\xe2\x80\x9d>\n<figcaption class=\xe2\x80\x9dattachment__caption\xe2\x80\x9d>\n<span class=\xe2\x80\x9dattachment__name\xe2\x80\x9d>Bla.pdf</span>\n<span class=\xe2\x80\x9dattachment__size\xe2\x80\x9d>635 KB</span>\n</figcaption>\n</figure></action-text-attachment>\n
Run Code Online (Sandbox Code Playgroud)\n\n

有什么想法可以让我下载这个吗?

\n\n

谢谢

\n

小智 5

你需要改变

app/views/active_storage/blobs/_blob.html.erb
Run Code Online (Sandbox Code Playgroud)

像这个

<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>
Run Code Online (Sandbox Code Playgroud)