Gemをインストールする
active_storage_validations
をインストールします。
Gemfile
gem 'active_storage_validations'
# :dimension または :aspect_ratioを使用する場合には以下のいずれかを併せてインストールします。
gem 'mini_magick', '>= 4.9.5'
gem 'ruby-vips', '>= 2.1.0'
ターミナル
bundle install
バリデーションを設定する
モデルにバリデーションを記述します。
app/models/user.rb
class User < ApplicationRecord
has_one_attached :avatar
validates :avatar, content_type: { in: %w[image/jpeg image/png] }, size: { less_than_or_equal_to: 2.megabytes }
end
上記の場合にはjpegおよびpngのファイルで2MB以下のものを許可します。