Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save almokhtarbr/535bd0f29fe35798d5ec406b91c964cd to your computer and use it in GitHub Desktop.
Save almokhtarbr/535bd0f29fe35798d5ec406b91c964cd to your computer and use it in GitHub Desktop.
validates image with active storage
validate :acceptable_image
def acceptable_image
return unless main_image.attached?
end
def acceptable_image
return unless main_image.attached?
unless main_image.byte_size <= 1.megabyte
errors.add(:main_image, "is too big")
end
end
def acceptable_image
return unless main_image.attached?
unless main_image.byte_size <= 1.megabyte
errors.add(:main_image, "is too big")
end
acceptable_types = ["image/jpeg", "image/png"]
unless acceptable_types.include?(main_image.content_type)
errors.add(:main_image, "must be a JPEG or PNG")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment