Acton文本,带有活动存储的显示视频播放器

问题描述 投票:1回答:1

我正在使用具有上传附件功能的Trix编辑器。

目前,我可以上传图像,pdf,视频等...,并在提交后显示。我也可以预览上传的图片。

但是我不知道如何向其中添加视频播放器。

我实际的_blob.html.erb看起来像这样:

<figure class="attachment attachment--<%= blob.representable? ? "preview" : "file" %> attachment--<%= blob.filename.extension %>">
    <% if blob.representable? %>
    <%= image_tag blob.representation(resize_to_fit: local_assigns[:in_gallery] ? [ 800, 600 ] : [ 1024, 768 ]) %>
    <% end %>

    <figcaption class="attachment__caption flex-1">
        <div>
        <% if caption = blob.try(:caption) %>
        <%= caption %>
        <% else %>
        <span class="attachment__name text-gray-600"><%= blob.filename %></span>
        <span class="attachment__size text-gray-600"><%= number_to_human_size blob.byte_size %></span>
        <% end %>
        </div>
         <div class="mt-6 mb-4">
        <%= link_to "View file", rails_blob_url(blob, disposition: "preview"), target: "_blank", class: "bg-gray-200 text-sm py-2 px-4 rounded mt-6" %> -
        <%= link_to "Download", rails_blob_path(blob, disposition: "attachment"), class: " text-sm bg-gray-200 py-2 px-4 rounded" %>
        </div>
    </figcaption>
</figure>
ruby-on-rails rails-activestorage trix actiontext
1个回答
0
投票

在您的_blob中:

 - if blob.video?
    %video(controls="true" width="100%" preload="metadata")
      %source{src: rails_blob_url(blob), type:"#{blob.content_type}"}

%video是HTML5视频标签

下一步,您需要将其添加到application.rb:

#video previews for action_text
config.after_initialize do
  ActionText::ContentHelper.allowed_attributes.add 'style'
  ActionText::ContentHelper.allowed_attributes.add 'controls'

  ActionText::ContentHelper.allowed_tags.add 'video'
  ActionText::ContentHelper.allowed_tags.add 'audio'
  ActionText::ContentHelper.allowed_tags.add 'source'
end

以上适用于rails 6.0.1,但可能不适用于rails 6.0.3

© www.soinside.com 2019 - 2024. All rights reserved.