I am working on implementing a feature for adding products to the backend. I have successfully added the product to the backend, from the frontend, but I am having trouble sending image of the product with the product data. I am using ActiveStorage to upload the image.
const formData = new FormData(); formData.append("product[image]", e.target.image.files[0]))
const formData = new FormData(); formData.append("product[image]", e.target.image.files[0]))
when the image is saved to the backend, I am not sure how to get the url for the image, so that I can send it to the frontend currently when products are sent to frontend, the image is not included in the response. I am not sure how to add the image to the response.
You can use has_many_attached declaration to attach multiple images to the products:
has_many_attached :images
Here is how you can get the displayable URL for an image attached to a product
:
Rails.application.routes.url_helpers.polymorphic_url(product.images.first)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.