LoginSignup
2

More than 5 years have passed since last update.

RefileをShrineにリプレイスするときのメモ

Last updated at Posted at 2018-12-28

これは何?

業務でRefileを使用しているプロダクトを触っているのですが、2018.12.28現在でRefileは1年近く更新が止まってしまっているので、Shrineに置き換えを行いました。その時のメモです。

shrineとは

Rails向け画像アップローダ。refileの後継にあたり、ダイレクトアップロードもできる。

README

shrine for refile users

作業手順

  1. shrineを使用したいモデルにinclude ImageUploader[:image]を追加する(これによって仮想属性のimageが使用できるようになる)

  2. shrineに置き換えたいモデルのDBにimage_dataカラムを追加する
    add_column :hoge_images, :image_data, :text

  3. migationファイルにimage_dataカラムへ値を追加する処理を追加する。

HogeImageModel.where(image_data: nil).each do |image|
  image.image = open(Rails.env.development? ? "https://local_path/#{image.src}" : image.src)
  image.image_attacher.finalize
  # finalizeメソッドがimage_dataの値をよしなに作成し、saveも行ってくれる
end
  1. viewのフォームをshrine用に書き直す
form_for @photo do |f|
  f.hidden_field :image, value: @photo.cached_image_data
  f.file_field :image
  f.submit
end

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2