【问题标题】:How can I upload multiple images using Active storage and jbuilder instead of 1 image?如何使用 Active storage 和 jbuilder 而不是 1 个图像上传多个图像?
【发布时间】:2020-07-17 18:02:32
【问题描述】:

这是我目前在我的 Vision 模型中拥有的:

    has_one_attached :image
    belongs_to :user

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

在我的 Vision 控制器中:

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


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

在 _vision.json.jbuilder 中:

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

json.image_url  polymorphic_url(vision.image)

我正在使用 Amazon S3 进行图像存储。如何更改我的代码以上传多个图像文件?

还请记住,我的数据库中有现有数据,我在其中获取每个视觉一个图像的图像 URL。如何在不影响现有数据库的情况下进行更改?

提前谢谢你?????????

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 rails-activestorage


    【解决方案1】:

    您需要在模型中指定has-many-attached,并且您的控制器需要准备好接收图像数组。像这样的:

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

    查看文档:https://edgeguides.rubyonrails.org/active_storage_overview.html#has-many-attached

    【讨论】:

    • @OzgeCokyasar 在您的问题中添加错误详细信息。像文档这样的方法是正确的方法。
    猜你喜欢
    • 2018-11-14
    • 2021-04-15
    • 1970-01-01
    • 2020-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-19
    • 1970-01-01
    相关资源
    最近更新 更多