Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@rails/activestorage

Package Overview
Dependencies
Maintainers
8
Versions
127
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.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
8
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 07 Nov 2024

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc