whatsapp_btn
whatsapp_btn Chat With Us

Home >> RoR >> Uploading files using Rails Active Storage

Uploading files using Rails Active Storage

  6 min read
Uploading files using Rails Active Storage

Quick Summary 

Rails Active Storage, a versatile framework, is purpose-built to adeptly handle file uploads, analysis, and seamless database storage within Ruby on Rails applications. This tutorial is crafted to provide you with a comprehensive understanding of how to execute file uploads using Active Storage in Ruby on Rails, offering a detailed, step-by-step guide.

The History of File Upload in Ruby on Rails

Rails 2.x Era

In the early stages of Ruby on Rails, file uploads were managed through plugins such as Paperclip and attachment_fu. Developers depended on third-party libraries to handle file attachments, which offered only a rudimentary i.e. very basic set of features for uploading and storing files.

Carrier wave Integration

CarrierWave, a versatile file upload library, got significant popularity within the Rails community. By 2011, integrating CarrierWave into Rails applications had become commonplace, providing developers with access to advanced features for image processing, storage backends, and customization.

Active Storage 

In April 2018, with the launch of Rails 5.2, the framework introduced Active Storage as an integrated solution for file uploads. This feature simplified the handling of file uploads by offering a standardized interface for seamless integration with various storage services, such as local disk storage, Amazon S3, and Google Cloud Storage.

Direct Uploads and DigitalOceanSpacesSupport

In 2019, Rails 6 unveiled direct uploads as a new feature of Active Storage. This capability empowers clients to upload files directly to cloud storage, thereby enhancing performance and alleviating server load. Additionally, Rails 6 expanded its support to include DigitalOcean Spaces as a storage service option, broadening the array of choices accessible to developers.

Resumable file upload

In December 2020, Rails 6.1 came out with a new feature: resumable file uploads. This means if your upload gets interrupted, you can start again from where you left off. It’s great for big files or if your internet connection is unreliable.

What is Active Storage?

Active Storage, a component of Rails and a pivotal aspect of Ruby Design patterns, streamlines file uploads in your applications. Seamlessly manage single or multiple uploads from your device, whether opting for local storage or cloud integration. It efficiently handles various file formats such as images, videos, PDFs, and documents

You don’t need to add a special field for files in every model with Active Storage. It’s a strong and flexible way to handle file uploads and attachments, with features like resizing images, uploading directly, and checking files.

Using Active Storage also helps keep your website running smoothly by keeping files separate from your database, making it easier to manage as your site grows.

You can also upload files directly to cloud services like Google Cloud, AWS S3, or Microsoft Azure. This means you won’t run out of space and can attach large files to your app’s records easily. And for testing and development, it comes with a simple way to use your computer’s storage.

Benefits of Active Storage

The benefits of Active Storage in Ruby on Rails:

  • Optimization: Ensures a secure and efficient process for uploading, serving, and analyzing files on cloud storage platforms.
  • Flexibility: Active Storage supports various cloud storage systems, providing flexible configuration and storage choices.
  • Integrated with Active Record: Seamlessly associates and manages data by attaching files to models.
  • Automate Handling: Automatically resizes images, adjusts file types, and schedules the expiration or deletion of uploaded files for streamlined management.

How to set up Active Storage 

Install and run migrations 

When setting up a new Rails project, Active Storage comes pre-installed. Simply install it and run migrations. This process will generate three new tables: active_storage_blobs, active_storage_attachments, and active_storage_variant_records.

  • active_storage_blobs: This table stores details about uploaded files, such as filename and content type.
  • active_storage_attachments: Acting as a polymorphic table, it links your model to blobs. If your model’s class name changes, you’ll need to perform a migration on this table to update the underlying record_type to reflect your model’s new class name.
  • active_storage_variant_records: This table stores different variants of images, if available.

Configure Services

To set up, declare services in the config/service.yml file. For every service your application utilizes, specify a name and the necessary configuration details.

Afterward, it’s necessary to clearly define and specify which service you intend to use for each specific environment in your application’s configuration.

How to Use Active Storage with Rails

Let’s learn how to use Active Storage in Rails with an illustrative example.

Associate Attachment 

Imagine we have a User model, and we want to save a profile picture for each user. To do this, we can use the “has_one_attached” method to link one picture with one user.

Attaching Multiple Files

In Rails with Active Storage, you can link many images to one thing. For example, if we have a post model and want to add several images to a post, we use the “has_many_attached” method. To make it work, we need to allow images as an array in the strong params section of our code. This makes handling file uploads in Rails smooth and easy.

Remove files from Rails active storage 

In managing attachments, you have the option to remove them using either the “purge” or “purge_later” method. The “purge” method deletes synchronously, while “purge_later” handles the deletion asynchronously, utilizing a job. Notably, there’s no need to create any specific job for Active Storage to manage this process effectively.

Generate URLs

To generate URLs for attachments, you have the convenience of using both the url_for and rails_blob_path methods. These methods ensure seamless generation of URLs for attachments in your Rails application.

Conclusion 

This tutorial serves as a guide for uploading files in Ruby on Rails using Active Storage. By following specific methods, you can install, configure, and utilize the Rails Active Storage System for file attachments and uploads. Implementing Active Storage in Ruby on Rails simplifies your file-uploading tasks, allowing you to concentrate on developing the core functionality of your applications.

If you require assistance or expert solutions, consider hiring our Ruby on Rails developer. They can assist you in uploading files in Ruby on Rails using Active Storage, streamlining the development process.

FAQ’S

If you need help with Active Storage or any aspect of Ruby on Rails development, consider hiring our Ruby on Rails developer who can guide you through the process and streamline your development workflow.

Active Storage optimizes file handling and storage, which can contribute to improved performance and user experience in Ruby on Rails applications.

Yes, Active Storage is designed to handle both small and large file uploads efficiently, making it suitable for a wide range of file sizes and types.

Absolutely, Active Storage seamlessly integrates with popular cloud storage services like Amazon S3, Google Cloud Storage, and Microsoft Azure, providing flexibility and scalability for file storage.

Tagline Infotech
Tagline Infotech a well-known provider of IT services, is deeply committed to assisting other IT professionals in all facets of the industry. We continuously provide comprehensive and high-quality content and products that give customers a strategic edge and assist them in improving, expanding, and taking their business to new heights by using the power of technology. You may also find us on LinkedIn, Instagram, Facebook and Twitter.

Related Posts :

contact-us-bg

Our Global Presence

India

Surat (HQ)

Digital Valley, 423, Apple Square, beside Lajamni Chowk, Mota Varachha, Surat, Gujarat 394101

Ahmedabad

D-401, titanium city center, 100 feet anand nagar road, Ahmedabad-380015

 +91 9913 808 285

U.S.A

1133 Sampley Ln Leander, Texas, 78641

United Kingdom

52 Godalming Avenue, wallington, London - SM6 8NW

U.A.E

Office No - 43-44, Al Fahidi, Bur Dubai, Dubai, United Arab Emirates

 +971 58 569 4786