All threads / How do I upload files with ActiveStorage and Vue.js?

Ask A Question


You’re not receiving notifications from this thread.

How do I upload files with ActiveStorage and Vue.js?

Taylor Cooney asked in Rails

Hey GoRailers,

Stemming from the Rails & Vue.js Trello Clone, I'm looking to add file uploads to a Card model using the new Rails 5.2 ActiveStorage feature. I'm stronger in Rails - previously I would use Paperclip to handle this. I've setup the config/storage.yml file and necessary migrations for ActiveStorage. I've also set the association between on the Card and updated the CardController to permit files: [].

The card.vue component built from the tutorial currently works well; a Card can have a title and description, and the description can be updated. These records persist to the database. My problem is figuring out how to bind file uploads to the card, and the logic required to upload multiple files on save.

Currently I'm using <input name="files" type="file" data-direct-upload-url="/rails/active_storage/direct_uploads" direct_upload="true" /> to create the input field in the card. However, after selecting a PDF image from my local machine and clicking save, the logs show that nothing happens [in regards to inserting a new row in active_storage_attachments or creating a blob]. How can I expand the save method to accept the file? Is the input field sufficient to bind the element?


class Card < ApplicationRecord
  has_many_attached :files


class CardsController < ApplicationController
    def card_params
      params.require(:card).permit(:list_id, :title, :position, :description, files: [])


        <div @click="editing=true" class="card card-body">

        <div v-if="editing" class="modal-backdrop show"></div>

        <div v-if="editing" @click="closeModal" class="modal show" style="display: block">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                    <div class="modal-body">
                        <textarea v-model="description" class="form-control"></textarea>
                    <div class="modal-footer">
                        <input name="files" type="file" data-direct-upload-url="/rails/active_storage/direct_uploads" direct_upload="true" />
                        <button @click="save" type="button" class="button button-secondary">Save changes</button>


export default {
    props: ["card", "list"],

    data: function() {
        return {
            editing: false,
            title: this.card.title,
            description: this.card.description,
            files: []

    methods: {
        save: function() {
            var data = new FormData
            data.append("card[title]", this.title)
            data.append("card[description]", this.description)

                url: `/cards/${}`,
                type: "PATCH",
                data: data,
                dataType: "json",
                success: (data) => {
                    const list_index = => ==
                    const card_index =[list_index].cards.findIndex((item) => ==
          [list_index].cards.splice(card_index, 1, data)

                    this.editing = false

Has anyone had any success with Vue and ActiveStorage in the past? I could really use some assistance in refactoring the form to handle file uploads.

Still looking for some assistance here 😅

I think it should be FormData() and not FormData. By just using will return you the function.

You need to go through this documentation.
Direct Uploads in Rails

The thing which is missing from the code is that you are just sending the title and description to your PATCH method in the cards controller. You are not sending any file related things like the signed ID for the blob of the file which you uploaded to the cloud server. Hence the database is not showing any uploads related information.

You should look at this discussion which might be helpful in understanding how direct upload works

Join the discussion

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

Join 38,558+ 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.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.