6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ActiveStorage::InvariableErrorについて

Posted at

画像を表示しているページでActiveStorage::InvariableErrorに遭遇したためまとめておきます。

ActiveStorage::InvariableErrorとは?

Railsのドキュメントには以下のように記載されていました。
「可変」という部分が微妙にわかりづらいため、記載されているそれぞれのメソッドを確認して行きます。

ActiveStorage::Blob#variant が可変でない blob に対して呼び出されたときに発生。ActiveStorage::Blob#variable?を使って、blobが可変かどうかを判断する。

ActiveStorage::Blob#variant

variantメソッドは画像のサイズや色を変更することができるメソッド。
以下の例では画像の高さと幅を100pxにリサイズしています。
(サイズだけでなく色も変更できるため「可変」と書かれていたんですね👀)

avatar.variant(resize_to_limit: [100, 100]).processed.url

ActiveStorage::Blob#variable?

variantメソッドによって変換可能な画像形式の場合にtrueを返します。
変換可能な画像形式はActiveStorage.variable_content_typesで確認ができるようになっています。

# Rails v6.1
ActiveStorage.variable_content_types
=> ["image/png",
 "image/gif",
 "image/jpg",
 "image/jpeg",
 "image/pjpeg",
 "image/tiff",
 "image/bmp",
 "image/vnd.adobe.photoshop",
 "image/vnd.microsoft.icon",
 "image/webp"]


つまり、変換ができない形式の画像に対してActiveStorage::Blob#variantを呼んだ時にActiveStorage::InvariableErrorが発生するようになっています。

エラーの原因と対策

今回エラーの原因となっていた画像を調べてみると、画像形式がimage/avifという形式になっており、その画像に対してActiveStorage::Blob#variantが呼ばれていました。※補足
対策としては、画像を添付したいModelにvaridationを設定することですが、ActiveStorageにはRailsの標準でvalidationを設定することができないっぽいです。

幸いactive_storage_validationsというgemがあるようなので、これを導入してvalidationを設定しておくと良さそう。
(なぜRailsの標準機能としてActiveStorageのvalidationを実装しないのだろうか?:thinking:

補足

今回はimage/avifの画像形式で例外が発生したわけですが、この画像形式は今後普及されていくであろうと言われている次世代型の画像形式ファイルらしいです。
もしやと思い、Rails7.0以降の環境で確認してみるとimage/avifにも対応していました。
「変換したい画像形式にActiveStorageが対応していない!」というときは、Railsのバージョンアップを検討してみるのも良いかもしれません。

ActiveStorage.variable_content_types
=> 
["image/png",
 "image/gif",
 "image/jpg",
 "image/jpeg",
 "image/pjpeg",
 "image/tiff",
 "image/bmp",
 "image/vnd.adobe.photoshop",
 "image/vnd.microsoft.icon",
 "image/webp",
 "image/avif", # !!!!
 "image/heic",
 "image/heif"]
6
3
0

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
6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?