As per the Rails guide documentations:
So, Active Storage basically allow user to upload attachments (i.e. Images) to the remote storage clouds and and save object of the file into Active Record.
Earlier with Rails 3 and Rails 4, We had to upload the files using carrierwave gem. and With Rails 5.2 and after versions Rails has inbuilt feature for file uploading to cloud and save it's object in Active Record using Active Storage module.
Which gem should be used for remote storage?
For Amazone web services (S3), you should use below gem:
For Azure Cloud storage, you should use below gem:
For Google Cloud Storage, you should use below gem:
Configuration:
To Store files locally
To Store files locally
Use it as:
Upload file:
Using gems and configure that, You should use below lines to attache the avatar.
Delete file:
user.avatar.purge
There are many other features included in it. Please go through the rails guide documentation.
Overall, It's a great feature included in rails and was most awaited since Rails 3.
For more information, Please visit: https://edgeguides.rubyonrails.org/active_storage_overview.html
Active Storage facilitates uploading files to a cloud storage service like Amazon S3, Google Cloud Storage, or Microsoft Azure Storage and attaching those files to Active Record objects. It comes with a local disk-based service for development and testing and supports mirroring files to subordinate services for backups and migrations.
So, Active Storage basically allow user to upload attachments (i.e. Images) to the remote storage clouds and and save object of the file into Active Record.
Earlier with Rails 3 and Rails 4, We had to upload the files using carrierwave gem. and With Rails 5.2 and after versions Rails has inbuilt feature for file uploading to cloud and save it's object in Active Record using Active Storage module.
Which gem should be used for remote storage?
For Amazone web services (S3), you should use below gem:
| gem "aws-sdk-s3", require: false | 
For Azure Cloud storage, you should use below gem:
| gem "azure-storage", require: false | 
For Google Cloud Storage, you should use below gem:
| 
 | 
Configuration:
To Store files locally
config.active_storage.service = :localTo Store files locally
| config.active_storage.service = :amazon | 
Use it as:
Upload file:
Using gems and configure that, You should use below lines to attache the avatar.
| user.avatar.attach(params[:avatar]) | 
Delete file:
user.avatar.purge
Using has_many_attached macros
The 
has_many_attached macro sets up a one-to-many relationship between records and files. Each record can have many files attached to it.
For example, suppose your application has a 
Product model. If you want each message to have many images, define the Product model like this:
You can create a product with images:
Call 
images.attach to add new images to an existing message:
Call 
images.attached? to determine whether a particular message has any images:
Ref: Rails Guide
There are many other features included in it. Please go through the rails guide documentation.
Overall, It's a great feature included in rails and was most awaited since Rails 3.
For more information, Please visit: https://edgeguides.rubyonrails.org/active_storage_overview.html
 


 
 
 
 
 
No comments:
Post a Comment