【问题标题】:Carrierwave upload and save to database only one fileCarrierwave 上传并保存到数据库仅一个文件
【发布时间】:2021-12-10 20:05:05
【问题描述】:

Carrierwave 始终只保存第一张图片。它在“更新”操作后工作,但显示错误:

“图像/上传/”的未定义方法 `reject'

它在 db 的 jsonb 字段中存储了一个简单的只有一个文件路径的字符串。但是为什么会这样,我该如何解决呢?

模型中的代码:

mount_uploaders :images, ImageUploader

上传者代码:

class ImageUploader < CarrierWave::Uploader::Base
  include Cloudinary::CarrierWave
  CarrierWave.configure do |config|
    config.cache_storage = :file
  end
  def extension_allowlist
    %w(jpg jpeg gif png)
  end
end

视图代码:

    = f.input :images, label: t("photos"), as: :file, input_html: { multiple: true, class: "form-control"}

    - @post.images.each do |image|
      = hidden_field :post, :images, multiple: true, value: image.identifier

    - if @post.images.any?
      .form-group
        %div.form-check
          = f.check_box :remove_images, label: t("posts.remove_images"), class: "form-check-input", id: "removeImages"
          %label.form-check-label{for: "removeImages"}
            = t("posts.remove_images", count: @post.images.count)

【问题讨论】:

    标签: ruby-on-rails ruby carrierwave


    【解决方案1】:

    发生这种情况是因为 Cloudinary::CarrierWave 不知道如何使用carrierwave 多文件上传功能(https://github.com/carrierwaveuploader/carrierwave#multiple-file-uploads) - 它不兼容。 Cloudinary::CarrierWave - 将字符串放入字段图像“img1”并仅保存一张图像,但carrierwave 需要图像数组['img1','img2'],这会为'str ...'引发未定义的方法'reject'

    如果您想与 cloudinary 一起使用,我建议您实现自己的多图像上传版本。或贡献于 cloudinary gem :D

    PS:我在本地查过

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 2020-01-28
      • 2013-09-26
      • 2013-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多