Shrine 中未定义的方法“image”

Undefined method `image' in Shrine

我尝试将 Shrine gem 添加到使用 Paperclip 的现有项目中。我在新模型中添加了 Shrine 图像(只是为了检查)。所以我用这个数据库创建了模型 Country table:

  create_table "countries", force: :cascade do |t|
    t.string "name"
    t.text "image_data"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

和这个模型:

class Country < ApplicationRecord
  include ImageUploader::Attachment(:image)

  validates :name, length: { in: 2..180 }, presence: true
end

在那之后我尝试从国家/地区获取图像并收到了这个(image_data 有效):

我想可能是因为图像是空的,但在另一个带有 shrine 的应用程序中一切正常

有人知道如何解决这个问题吗?

似乎在 shrine.rb 文件之前以某种方式加载了 shrine 上传器,因此我无法使用任何 shrine 方法。可以在这里找到类似的问题:https://github.com/shrinerb/shrine/issues/155

require_relative '../../config/initializers/shrine' 添加到上传器解决了问题

我在 shrine 版本 3.2.1 中遇到错误,并通过在 shrine.rb 中添加 Shrine.plugin :activerecord 来修复它。希望对你有帮助。

https://shrinerb.com/docs/plugins/activerecord