简体   繁体   中英

Undefined method `image' in Shrine

I try to add Shrine gem to an existing project that uses Paperclip. I added a Shrine image to a new model (just for a check). So I created model Country with this db 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

and this model:

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

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

After that I tried to get image from country and received this (image_data works, though):

在此处输入图像描述

I thought that maybe it's because image is empty, but in another app with shrine everything works correctly

在此处输入图像描述

Does anyone know how to fix this?

It seems that somehow shrine uploader was loaded before shrine.rb file so that I couldn't use any shrine methods. Similar issue can be found here: https://github.com/shrinerb/shrine/issues/155

Adding require_relative '../../config/initializers/shrine' to the uploader resolved the issue

I got the error in shrine version 3.2.1 and fixed it by adding Shrine.plugin:activerecord in shrine.rb . hope it can help.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM