我正在从回形针迁移到Rails 5.2和活动存储。我使用rails作为API-only。
如何获取has_many_attached的URL路径:图像
以下是运行的单个文件的代码:
class UserSerializer < ActiveModel::Serializer
include Rails.application.routes.url_helpers
attributes %i[email name username]
....
attribute :verification_url do
if object.verification_file.attachment
URI.join(ActionController::Base.asset_host, rails_blob_path(object.verification_file))
end
end
....
end
当我尝试为多个图像做类似的事情时,我只是得到这些图像,而不是它们的URL。
include Rails.application.routes.url_helpers
attributes :id, :name, :description, :images
def images
if object.images.attachments
object.images.each do |image|
URI.join(ActionController::Base.asset_host, rails_blob_path(image))
end
end
end
https://stackoverflow.com/questions/50473730
复制相似问题