0

我正在尝试使用 Shrine 将图像上传到谷歌驱动器。我按照这个宝石的说明 https://github.com/verynear/shrine-google_drive_storage

我将这些宝石添加到我的项目中

gem 'shrine'
gem 'shrine-google_drive_storage'

这是我的 config/shrine.rb

require "shrine"
require "shrine/storage/google_drive_storage"

Shrine.storages = {
  cache: Shrine::Storage::GoogleDriveStorage.new(prefix: "cache"),
  store: Shrine::Storage::GoogleDriveStorage.new(prefix: "store"),
}

Shrine::Storage::GoogleDriveStorage.new(
  prefix: "store",
  google_drive_client_secret_path: "#{Rails.root}/config/client_secret.json",
  drive_public_folder_id: '0Bz_kkknizZmZRXZzeXdua1FNUXc',
  google_drive_options: {
        path: proc { |style| "#{id}_#{photo.original_filename}_#{style}" },
      },
)

但是当我尝试上传图片时,我收到此错误消息

NameError (undefined local variable or method `content_type' for #<Shrine::Storage::GoogleDriveStorage:0x00000004009868>):

有人知道我在哪里犯错了吗?

4

1 回答 1

0

您应该使用所有必需的选项进行初始化Shrine::Storage::GoogleDriveStorage并分配给相应的 Shrine 存储(cachestore),如下所示:

Shrine.storages = {
  cache: Shrine::Storage::GoogleDriveStorage.new(
    prefix: "cache",
    google_drive_client_secret_path: "#{Rails.root}/config/client_secret.json",
    drive_public_folder_id: '0Bz_kkknizZmZRXZzeXdua1FNUXc',
    google_drive_options: {
      path: proc { |style| "#{id}_#{photo.original_filename}_#{style}" }
    }
  ),
  store: Shrine::Storage::GoogleDriveStorage.new(
    prefix: "store",
    google_drive_client_secret_path: "#{Rails.root}/config/client_secret.json",
    drive_public_folder_id: '0Bz_kkknizZmZRXZzeXdua1FNUXc',
    google_drive_options: {
      path: proc { |style| "#{id}_#{photo.original_filename}_#{style}" }
    }
  )
}

之后,您需要创建一个上传器类,ImageUploader并在模型中引入一个附件属性。有关详细说明,请按照janko-m/shrine 快速入门部分中的步骤来适应您的需要。

于 2017-11-03T20:02:07.427 回答