我正在使用具有上传附件功能的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>
在您的_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