Save 36% for Black Friday! Learn more

All threads / Active Storage Rails 6 API unable to insert to active_storage_attachments table
Ask A Question


You’re not receiving notifications from this thread.

Active Storage Rails 6 API unable to insert to active_storage_attachments table

Dondon Abesamis asked in Rails

I am creating API endpoint for active storage with reactjs. My upload controller is as follows:
The code belows execute:

insert blob object in active_storage_blob - ok
Uploaded file to the designated path in storage - ok
does not insert active_storage_attachments (polymorphic relation) I already add has_one_attached :photo

   class Api::V1::Activestorage::DirectUploadsController <  ActiveStorage::DirectUploadsController
   skip_before_action :verify_authenticity_token

    def create
       blob = ActiveStorage::Blob.create_before_direct_upload!(blob_args)
       render json: direct_upload_json(blob)


    def blob_args
     params.require(:blob).permit(:filename, :byte_size, :checksum, :content_type, metadata: {}).to_h.symbolize_keys

       def direct_upload_json(blob)
             blob.as_json(root: false, methods: :signed_id).merge(direct_upload: {
            url: blob.service_url_for_direct_upload,
            headers: blob.service_headers_for_direct_upload

Hope anyone can help me thanks

Using Active Storage in Rails 6, Active Storage has been merged into Rails! You can find the latest version of the source code inside the Rails repository, ...

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 67,819+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2022 GoRails, LLC. All rights reserved.