Is it possible to add a validation to accept only .pdf and .doc files using Active Storage?
Active Storage uses two tables in your application's database named active_storage_blobs and active_storage_attachments . After creating a new application (or upgrading your application to Rails 5.2), run rails active_storage:install to generate a migration that creates these tables.
image. service_url . This will give you the url where the image is saved. I.E.
1 What is Active Storage? Active Storage facilitates uploading files to a cloud storage service like Amazon S3, Google Cloud Storage, or Microsoft Azure Storage and attaching those files to Active Record objects.
Adding a gem for just a few lines of code is not always needed. Here's a working example based on a small alteration on the example above.
validate :logo_content_type, if: -> { logo.attached? }
def logo_content_type
allowed = %w[image/jpeg image/jpeg image/webp image/png]
if allowed.exclude?(logo.content_type)
errors.add(:logo, message: 'Logo must be a JPG, JPEG, WEBP or PNG')
end
end
there is a gem which provides validation for active storage
gem 'activestorage-validator'
https://github.com/aki77/activestorage-validator
validates :avatar, presence: true, blob: { content_type: :image }
validates :photos, presence: true, blob: { content_type: ['image/png', 'image/jpg', 'image/jpeg'], size_range: 1..5.megabytes }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With