vendredi 17 juillet 2020

How can I upload multiple images using Active storage and jbuilder instead of 1 image?

This is what I currently have in my Vision model:

    has_one_attached :image
    belongs_to :user

    def featured_image_url
        if self.image.attachment
          self.image.attachment.service_url
        end
      end
end

in my Vision controller:

    def create
        @vision = current_user&.visions.create(vision_params)
        render :create, status: :created
    end


    def vision_params
        params.permit(:description, :image)
    end

and in _vision.json.jbuilder:

json.call(
    vision,
    :id,
    :description,
    :created_at,
    :updated_at
)

json.image_url  polymorphic_url(vision.image)

I am using Amazon S3 for image storage. How can I change my code to be able to upload multiple image files?

Please also bear in mind that I have existing data in my database where I am getting an image URL for one single Image per Vision. How can I make the changes without affecting my existing database?

Thank you in advance đŸ™đŸŒ

Aucun commentaire:

Enregistrer un commentaire