Rails:错误上传多个文件,空数组

时间:2018-12-22 17:26:23

标签: ruby-on-rails carrierwave cloudinary

我有个大问题,我的Rails应用程序无法正确上传照片,每次都会出现此错误。

您可以在照片右边看到无法上传cloudinary网址

a busy cat

自管理员以来,我必须这样做,而不是从网站本身来添加图片

cars_controller.rb

def index
 @cars = Car.all()
end

def show
 @car = Car.friendly.find(params[:id])
 @photos = @car.photos
 end
end

photos_uploader.rb

 include Cloudinary::CarrierWave

 def store_dir
 "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
 end

 def extension_whitelist
  %w(jpg jpeg gif png)
 end

photo.rb

mount_uploaders :photos, PhotosUploader
belongs_to :car

car.rb

extend FriendlyId
friendly_id :title, use: :slugged

mount_uploader :photo, PhotoUploader

has_many :photos

您可以看到我的错误

error

1 个答案:

答案 0 :(得分:0)

Cloudinary Ruby gem当前不支持

mount_uploaders。可以在此处https://github.com/cloudinary/cloudinary_gem/issues/304跟踪该问题。

有一个示例项目(https://github.com/taragano/Cloudinary_multiple_uploads)说明了一个临时解决方法。这些模型是相册和照片,其中相册与照片具有has_many关系,照片与相册具有belongs_to关系。

模型(https://github.com/taragano/Cloudinary_multiple_uploads/tree/master/app/models)应该看起来像这样:

class Photo < ActiveRecord::Base
    belongs_to :album
    mount_uploader :image, ImageUploader
end

class Album < ActiveRecord::Base
    has_many :photos, dependent: :destroy
end

在控制器(https://github.com/taragano/Cloudinary_multiple_uploads/blob/master/app/controllers/albums_controller.rb)中,您需要遍历每张照片并将其存储到它所属的相册中:

def store_photos
    photos = params[:album][:photos]
    photos.each{|photo| @album.photos.create(image: photo)} if photos
end