【发布时间】:2019-06-05 22:31:39
【问题描述】:
我有一个仅 API 的应用程序,它使用“fast_jsonapi”GEM 呈现 JSON 响应。
我设法使用 ActiveStorage 上传视频,并且可以在 JSON 响应中发送视频 URL。但我需要在同一个响应中发送一个缩略图 URL。
我查看了 Rails 文档和 Google,发现有一些方法可以在 Rails 应用程序上完成
<ul>
<% @message.files.each do |file| %>
<li>
<%= image_tag file.preview(resize_to_limit: [100, 100]) %>
</li>
<% end %>
</ul>
但我找不到如何将其包含到仅 API 的应用程序中并在 JSON 响应中发送缩略图 URL。
现在这是我的序列化程序,我正在尝试在博客上找到的解决方案,但到目前为止它不起作用。我不确定这是否是正确的方法,或者我该如何解决这个问题。
class EjercicioSerializer
include FastJsonapi::ObjectSerializer
#include ActionView::AssetPaths
include ActionView::Helpers::AssetTagHelper
set_id :id
attributes :nombre, :descripcion
attribute :video_url do |object|
Rails.application.routes.url_helpers.rails_blob_path(object.video, only_path: true) if object.video.attachment
end
attribute :video_thumbnail do |object|
link_to(image_tag(object.video.preview(resize: "200x200>")),
Rails.application.routes.url_helpers.rails_blob_path(object.video, disposition: "attachment"))
end
end
【问题讨论】:
标签: ruby-on-rails rails-api rails-activestorage video-thumbnails