You're Invited: Meet the Socket team at BSidesSF and RSAC - April 27 - May 1.RSVP
Socket
Sign inDemoInstall
Socket

@rails/activestorage

Package Overview
Dependencies
Maintainers
0
Versions
133
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@rails/activestorage

Attach cloud and local files in Rails applications

8.0.200
latest
Source
npm
Version published
Maintainers
0
Created

What is @rails/activestorage?

@rails/activestorage is a JavaScript package that integrates with Rails to handle file uploads. It provides a straightforward way to upload files directly to cloud storage services like Amazon S3, Google Cloud Storage, or Microsoft Azure Storage. It also supports direct uploads from the client-side to these services, bypassing the Rails server.

What are @rails/activestorage's main functionalities?

Direct Uploads

This feature allows you to upload files directly from the client-side to the cloud storage service, bypassing the Rails server. This can improve performance and reduce server load.

import { DirectUpload } from '@rails/activestorage';

const input = document.querySelector('input[type=file]');
input.addEventListener('change', (event) => {
  const file = event.target.files[0];
  const upload = new DirectUpload(file, '/rails/active_storage/direct_uploads');

  upload.create((error, blob) => {
    if (error) {
      console.error('Upload failed:', error);
    } else {
      console.log('Upload successful:', blob);
    }
  });
});

Attaching Files to Models

This feature allows you to attach uploaded files to Rails models. The uploaded file's signed ID is stored in a hidden field, which is then submitted with the form.

import { DirectUpload } from '@rails/activestorage';

const input = document.querySelector('input[type=file]');
input.addEventListener('change', (event) => {
  const file = event.target.files[0];
  const upload = new DirectUpload(file, '/rails/active_storage/direct_uploads');

  upload.create((error, blob) => {
    if (error) {
      console.error('Upload failed:', error);
    } else {
      const hiddenField = document.createElement('input');
      hiddenField.setAttribute('type', 'hidden');
      hiddenField.setAttribute('value', blob.signed_id);
      hiddenField.name = 'model[attachment]';
      document.querySelector('form').appendChild(hiddenField);
    }
  });
});

Progress Tracking

This feature allows you to track the progress of file uploads. You can provide feedback to users about the upload status, which can improve the user experience.

import { DirectUpload } from '@rails/activestorage';

const input = document.querySelector('input[type=file]');
input.addEventListener('change', (event) => {
  const file = event.target.files[0];
  const upload = new DirectUpload(file, '/rails/active_storage/direct_uploads');

  upload.create((error, blob) => {
    if (error) {
      console.error('Upload failed:', error);
    } else {
      console.log('Upload successful:', blob);
    }
  });

  upload.upload((progress) => {
    console.log(`Progress: ${progress}%`);
  });
});

Other packages similar to @rails/activestorage

FAQs

Package last updated on 12 Mar 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts