2

我试图简单地显示我附加到我的用户模型的图像的 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_urlorurl方法:-

  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 中不容易做到。

4

1 回答 1

2

解决方案是通过添加以下内容来设置主机:-

include ActiveStorage::SetCurrent

在我的控制器中。

于 2021-04-25T16:04:14.473 回答