Rails神社图片上传到谷歌驱动器错误

时间:2017-11-03 13:32:23

标签: ruby-on-rails ruby shrine

我正在尝试使用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>):

有人知道我弄错了吗?

1 个答案:

答案 0 :(得分: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 quickstart section中的步骤进行操作以适应您的需要。