Last active
February 29, 2024 12:25
-
-
Save sulmanweb/aae082a30c3b7f2264895f7376ecdca8 to your computer and use it in GitHub Desktop.
ActiveStorage file uploading in GraphQL API in Rails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/document.rb | |
class Document < ApplicationRecord | |
## RELATIONSHIPS | |
has_one_attached :doc | |
belongs_to :documentable, polymorphic: true, optional: true | |
belongs_to :user, optional: true | |
## VALIDATIONS | |
validate :doc_presence, on: :create | |
# This validation validates that attachment can be any image of pdf | |
def doc_presence | |
pattern = %r{^(image|application)/(.)+$} | |
unless doc.attached? && pattern.match?(doc.attachment.blob.content_type) | |
errors.add(:doc, I18n.t("errors.models.document.file_presence")) | |
end | |
end | |
end |
I'm sorry; I mostly work on Backend. I won't be able to help much. But my front-end colleagues solved it in VueJS. But it will be difficult to ask them.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@happycamper1129 https://github.com/jaydenseric/apollo-upload-client this is client part of ApolloUploadServer Ruby library