简体   繁体   English

如何使用 Active storage 和 jbuilder 上传多张图片而不是一张图片?

[英]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:这是我目前在 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:在我的愿景 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:在 _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.我正在使用 Amazon S3 进行图像存储。 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.另请记住,我的数据库中有现有数据,我正在为每个视觉的单个图像获取图像 URL。 How can I make the changes without affecting my existing database?如何在不影响现有数据库的情况下进行更改?

Thank you in advance先感谢您

You need specify has-many-attached in your model and your controller need be prepare to receive a array of images.您需要在 model 中指定has-many-attached并且您的 controller 需要准备接收一系列图像。 Something like this:是这样的:

def vision_params
  params.permit(:description, :images, [)
end

See the doc: https://edgeguides.rubyonrails.org/active_storage_overview.html#has-many-attached请参阅文档: https://edgeguides.rubyonrails.org/active_storage_overview.html#has-many-attached

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM