Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Change default file path of Active storage in Ruby on Rails 5

Change default file path of active storage in ruby on Rails 5

Active Storage

Active Storage makes it simple to upload and reference files in cloud services, 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 which supports mirroring files to subordinate services for backups and migrations.

Files can be uploaded from the server to the cloud or directly from the client to the cloud.

Image files can further more be transformed using on-demand variants for quality, aspect ratio, size, or any other MiniMagick supported transformation.

Before starting this tutorial, I assume that you have Basic Rails application created.

Installation and migration

1 Add gem “activestorage”, git: “https://github.com/rails/activestorage.git" to your Gemfile.

2 Add require “active_storage” to config/application.rb, after require “rails/all” line.

3 Optional: Add gem “mini_magick” to your Gemfile if you want to use variants.

Setup

Active Storage uses two tables in your application’s database named active_storage_blobsand active_storage_attachments. After creating a new application (or upgrading your application to Rails 5.2), run rails active_storage:install to generate a migration that creates these tables. Use rails db:migrate to run the migration.

Default Active storage Image Saving Path

Declare Active Storage services in config/storage.yml. For each service your application uses, provide a name and the requisite configuration. The example below declares three services named local, test, and amazon:

Dynamic Active storage Image Saving Path

Tell Active Storage which service to use by setting Rails.application.config.active_storage.service. Because each environment will likely use a different service, it is recommended to do this on a per-environment basis. To use the disk service from the previous example in the development environment, you would add the following to config/environments/development.rb:

Store file locally:

Example:

Routes

Model

new.html.erb

show.html.erb

profile_info.js

Controller

At BoTree, we build web and mobile applications to add value to our client’s business. We align ourselves to ensure that our client benefits the most out of our engagement.

We work in Ruby on Rails, Python, Java, React, Android and iOS.

Drop us a line to discuss how can we help take your business to the next level.


Change default file path of Active storage in Ruby on Rails 5 was originally published in BoTree Technologies on Medium, where people are continuing the conversation by highlighting and responding to this story.



This post first appeared on BoTreeTechnologies Official, please read the originial post: here

Share the post

Change default file path of Active storage in Ruby on Rails 5

×

Subscribe to Botreetechnologies Official

Get updates delivered right to your inbox!

Thank you for your subscription

×