I want to return the URL of the image (has_many_attached) saved in ActiveStorage with json.
It does n’t work.
The reason is that url_for cannot be stored in the array.
The JSON I want to return is below.
Thank you in advance.

      "id": 10,
      "uuid": null,
      "title": null,
      "detail": null,
class Image
class ImagesController
  • Answer # 1

    has_many_attached: myImage There is more than one myImage?
    If you set it as has_many_attached: myImages, the later mistakes will be reduced.

    I think it won't work because url_for (myImage) is passed an array (something like).
    I've never used url_for (myImage) like that, so I can go with it.
    myImages.map {| myImage | url_for (myImage)}
    If myImage exists but may not have myImage.attached?
    myImages.map {| myImage | url_for (myImage) if myImage.attached?} .compadt

Related articles