[RUBY] [Active Storage] Validation settings when uploading files

Overview

Since Active Storage does not have default validation, it records the time when you set the validation by yourself as a memorandum.

environment

・ Ruby '2.5.7' ・ Rails '5.2.3'

process

1. Preparation

Execute the following command to install Active Storage.

$ rails active_storage:install

A migration file is created that creates the following two tables. ・ Active_storage_attachments ・ Active_storage_blobs

Rails db: migrate to create the table.

$ rails db:migrate

2. Association to model

We will associate it with the model. Here, it is assumed that multiple profile images are set in the User model.

models/user.rb


class User < ApplicationRecord
  has_many_attached :avatars
end

: avatars is the name of the file, and you can specify whatever you like, such as : photos, : images, etc. according to the purpose of the file. There is no need to provide columns for images in the User model.

3. Validation settings

We will set the validation for the User model. Here, we will make the following three settings.

・ Avatar_type Specify the file format that can be uploaded.

・ Avatar_size Specify the size (capacity) of one file that can be uploaded.

・ Avatar_length Specify the number of files that can be uploaded.

models/user.rb


class User < ApplicationRecord
(abridgement)
  validate :avatar_type, :avatar_size, :avatar_length

  private

  def avatar_type
    avatars.each do |avatar|
      if !avatar.blob.content_type.in?(%('image/jpeg image/png'))
        avatar.purge
        errors.add(:avatars, 'Please upload in jpeg or png format')
      end
    end
  end

  def avatar_size
    avatars.each do |avatar|
      if avatar.blob.byte_size > 5.megabytes
        avatar.purge
        errors.add(:avatars, "Must be within 5MB per file")
      end
    end
  end

  def avatar_length
    if avatars.length > 4
      avatars.purge
      errors.add(:avatars, "Please keep within 4 sheets")
    end
  end
end

By the way, I am deleting temporary data with `ʻavatars.purge``. If a validation error occurs in other input items, the uploaded file will be saved in the storage when it is assigned to the attribute of the model instance in Rails 5.2 to 6.0 series or earlier, and as a result, it will be in the blob. This is because temporary data for errors may accumulate. (However, there was a change in the function of Active Storage in the update to Rails 6.0, and after the save method was executed, it was changed to the specification to be saved in the storage, so it is unnecessary if it is 6.0 or later)

result

Now you have the following validation settings! 200919_バリデーションエラー.png

reference

Consider whether Active Storage can be used as a substitute for Carrier Wave Implement image upload function using Rails5 Active Storage

Recommended Posts

[Active Storage] Validation settings when uploading files
Rails Active Storage shrinks images before uploading
Introduce Active Storage
About Active Storage
[Rails 6] Add images to seed files (using Active Storage)
About DelegationError (Active Storage)
Active Storage Initialization Manual
[Beginner] About Active Storage
A mistake made when displaying an image using Active Storage
[Rails] Create API to download files with Active Storage [S3]