Ruby / Rails-Shrine :: Error(存储:store未在ImageUploader上注册)

在过去的几天里,我试图为我的应用程序添加一个新的存储配置,因此我们可以将Cloudinary用于图像,将Digital Ocean Spaces用于其他文件。 在进行了我认为需要的更改之后,当我尝试访问与ImageUploader的附件关联的旧模型的属性时,出现以下错误:

Shrine::Error (storage :store isn't registered on ImageUploader)

问题是:确实没有定义存储“存储”,但这是因为我试图拥有一个image_store和一个raw_store

这是我的config/initializers/shrine.rb

# config/initializers/shrine.rb
require "shrine"
require "shrine/storage/file_system"
require "shrine/storage/cloudinary"
require "shrine/storage/s3"

Shrine.plugin :activerecord
Shrine.plugin :cached_attachment_data # for retaining the cached file across form redisplays
Shrine.plugin :restore_cached_data # re-extract metadata when attaching a cached file
Shrine.plugin :rack_file # for non-Rails apps
Shrine.plugin :default_storage,store: :image_store

s3_options = {
  access_key_id: ENV['DO_SPACE_accESS_KEY_ID'],secret_access_key: ENV['DO_SPACE_SECRET_accESS_KEY'],bucket: ENV['DO_SPACE_SECRET_BUCKET'],endpoint: "https://#{ENV['REGION']}.digitaloceanspaces.com",region: ENV['REGION']
}

if Rails.env.development? || Rails.env.test?
  Shrine.storages = {
    raw_cache: Shrine::Storage::FileSystem.new("public",prefix: "uploads/raw_cache"),# temporary
    raw_store: Shrine::Storage::FileSystem.new("public",prefix: "uploads/raw_store"),# permanent
    image_cache: Shrine::Storage::FileSystem.new("public",prefix: "uploads/image_cache"),# temporary
    image_store: Shrine::Storage::FileSystem.new("public",prefix: "uploads/image_store")       # permanent
  }
else
  Shrine.storages = {
    raw_cache: Shrine::Storage::S3.new(
      prefix: "raw_cache",upload_options: { acl: 'public-read' },**s3_options
    ),# for direct uploads
    raw_store: Shrine::Storage::S3.new(
      prefix: "raw_store",image_cache: Shrine::Storage::Cloudinary.new(prefix: "image_cache",resource_type: 'image'),# for direct uploads
    image_store: Shrine::Storage::Cloudinary.new(prefix: "image_store",}
end

这是我的ImageUploader:

class ImageUploader < Shrine
  plugin :default_storage,cache: :image_cache,store: :image_store
  plugin :validation_helpers
  plugin :data_uri
  plugin :infer_extension

  plugin :upload_options,store: ->(io,context) do
    [...]
  end

  plugin :url_options,store: -> (io,**options) do
    [...]
  end

  # I even tried that one,but no success
  Attacher.default_store :image_store

  Attacher.validate do
    validate_max_size 5*1024*1024,message: "is too large (max is 5 MB)"
  end
end

有人可以帮我吗?

iCMS 回答:Ruby / Rails-Shrine :: Error(存储:store未在ImageUploader上注册)

好的,我找到了问题。

起初,我的Shrine初始化程序中以前只有2个存储空间:

[...]
Shrine.storages = {
  cache: Shrine::Storage::Cloudinary.new(prefix: "image_cache",resource_type: 'image'),# for direct uploads
  store: Shrine::Storage::Cloudinary.new(prefix: "image_store",}
[...]

由于这个原因,现在我要更新存储,当我恢复以前保存的记录时,将其与“存储:存储”保存在附件字​​段中,该字段不再与旧存储的存储匹配云配置。

所以,看来我需要做的是:更新数据库中我拥有的任何文件列的存储字段,以使其与当前配置匹配。

本文链接:https://www.f2er.com/2274380.html

大家都在问