Search code examples
jqueryruby-on-railsrails-activestorage

How can i reference the DirectUpload js class from my jQuery block?


My layout has:

//= require activestorage

Now in my jQuery I am trying to reference the DirectUpload class:

  // Instantiate the DirectUploader object
  const upload = new DirectUpload(file, url)

The direct upload class is here: https://github.com/rails/rails/blob/b2eb1d1c55a59fee1e6c4cba7030d8ceb524267c/activestorage/app/javascript/activestorage/direct_upload.js

I am currently getting this error:

Uncaught ReferenceError: DirectUpload is not defined at new:210


Solution

  • You can reference the DirectUpload class using the ActiveStorage namespace.

    For example,

    new class ActiveStorage.DirectUpload(...)