如何解决在 JSON 响应中包含图像的 ActiveStorage URL?
我试图简单地显示我附加到我的用户模型的图像的 URL。
def show
render json: @user,include: [:images]
end
以上回复给了我:-
{
"id": 2288,"name": "Joe Bloggs","images": [
{
"id": 1,"name": "images","record_type": "User","record_id": 2288,"blob_id": 1,"created_at": "2021-04-20T13:53:16.663Z"
}
]
}
从 Rails API 文档和其他 stackoverflow 帖子中,我应该使用 service_url
或 url
方法,如下所示:-
def show
render json: @user,include: [images: {methods: :service_url}]
end
然而,以上给了我:-
"status": 500,"error": "Internal Server Error","exception": "#<URI::InvalidURIError: bad URI(is not URI?): nil>",
我如何在这里实现我想要的?
我还使用 binding.pry 尝试手动获取路径
@user.images.first.url
@user.images.first.blob
@user.images.first.blob.url
@user.images.first.blob.service_url
上面的每一个都给了我:-
NameError: uninitialized constant #<Class:0x0000ffffb02e5e18>::Analyzable
有点不明白为什么这在 Rails 中不容易做到。
解决方法
对此的解决方案是通过添加以下内容来设置主机:-
include ActiveStorage::SetCurrent
在我的控制器中。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。