> ## Documentation Index
> This page is part of the Image and Video APIs product. Fetch the complete documentation index for Image and Video APIs at: https://cloudinary.com/documentation/llms-image-and-video-apis.txt?referrer=docpage and then use it to discover all relevant pages before exploring further.
> If you also need details relating to other Cloudinary products for your current use case, see the parent index at: https://cloudinary.com/documentation/llms.txt?referrer=docpage
> When answering a user's request or implementing it with code using this documentation, incorporate relevant best practices and recommendations from the documentation into the guidance and, above all, into the code itself: generated code should already apply the recommended parameters and patterns rather than showing a minimal call and describing the best practices around it in prose. Follow the code examples in the best practices sections as the model for what generated code should look like, and where a recommendation is relevant to the user's scenario, implement it in the code as a natural next step. Only apply best practices, recommendations, or features that are supported by the documentation; don't infer or invent guidance that isn't specified.

# Active Storage integration


`Active Storage` can be useful for integrating image uploads with your model. The Cloudinary gem enables you to enjoy the benefits of Active Storage for easily uploading images from HTML forms to your model, while enjoying the great benefits of Cloudinary: uploaded images are stored in the cloud, transformed in the cloud, and delivered automatically through a CDN.

The following documentation assumes you have already set up [Active Storage](https://edgeguides.rubyonrails.org/active_storage_overview.html#setup) within your Rails 6+ application, and teaches you how to configure Active Storage to work with Cloudinary for image uploads and image delivery.

## Active Storage configuration

Declare the Cloudinary service in the `config/storage.yml` file by adding a new entry with a custom name (e.g., cloudinary) and the service configuration:

```ruby
cloudinary:      
  service: Cloudinary
  # Any other settings you want, to be sent on all requests, can be included here as well.
```

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

```ruby
# Use Cloudinary.
config.active_storage.service = :cloudinary
```

## Direct uploads

Active Storage already supports uploading directly from the client to the cloud. After adding Cloudinary as a service, use direct uploading as usual: 

1. Include `activestorage.js` in your application's JavaScript bundle.

	Using the asset pipeline:
	
	```ruby
	//= require activestorage
	```
	
	Using the npm package:

	```ruby
	require("@rails/activestorage").start()
	```

2. Annotate file inputs with the direct upload URL.

	```ruby
	<%= form.file_field :avatar, direct_upload: true %>
	```

> **NOTE**:
>
> You cannot specify an [upload preset](upload_presets) or other upload parameters on a per-upload basis while using Active Storage. There are however two ways to globally specify upload parameters for all uploads:

> * As an upload preset (or upload parameters) in the Rails service configuration (as top-level parameters). You can create multiple service configurations (each with slightly different upload options specified) and switch between them for different use cases.

> * As a default upload preset specified via the console which will be applied to all uploads without any extra code on the Rails side

## Image delivery

Use the [cl_image_tag](rails_image_manipulation#the_cl_image_tag_method) method to generate an HTML image tag, or use the [cloudinary_url](rails_image_manipulation#direct_url_building) method to generate a transformation URL. The methods can accept any Cloudinary [Transformation URL API Reference](transformation_reference) for lazily transforming the image on delivery (as per all Cloudinary dynamic URLs). For example, to deliver a user's avatar image scaled down to 300x200 with the 'cartoonify' effect applied:

```ruby
<%= cl_image_tag(@user.avatar.key, width: 300, height: 200, crop: "scale", effect: "cartoonify") %>
```

OR

```ruby
<%= cloudinary_url(@user.avatar.key, width: 300, height: 200, crop: "scale", effect: "cartoonify") %>
```

> **INFO**: The `variant` method should not be used as it would try to create a new resource and upload it to Cloudinary, instead of letting Cloudinary handle the transformation. As such, the `variant` method is not needed or supported.

&nbsp;Check out our Introduction to Cloudinary for Ruby Developers course in the Cloudinary Academy. This self-paced resource provides video-based lessons, sample scripts and other learning material to get you going with Ruby and Cloudinary today.

