LoginSignup
3
2

More than 3 years have passed since last update.

CarrierWaveでファイルサイズをバリデーションする(1行で)

Last updated at Posted at 2021-03-12

こちらの記事のトリビュート
https://qiita.com/tkiha/items/20216451845be2e4b1aa

gemを使う方法もあるので、それの紹介。

1). gemを入れる

gem 'file_validators'

2). モデルに記述(例)

validates :image, file_size: { less_than: 2.gigabytes }

Rspecをどう書いたかmemo

上の例のバリデーションを入れているとして、テストするのに2ギガバイトのファイルを用意するのはしんどいので、
モックにしました。

allow(some_model.image).to receive(:size).and_return(2.gigabytes)
3
2
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
3
2