uploadcare-rails icon indicating copy to clipboard operation
uploadcare-rails copied to clipboard

Rails API client that handles uploads and further operations with files by wrapping Uploadcare Upload and REST APIs.

Uploadcare Rails

license Build Status

A Ruby on Rails plugin for Uploadcare service. Based on uploadcare-ruby gem (general purpose wrapper for Uploadcare API)

:heavy_exclamation_mark: Note: the gem uploadcare-rails 2.x is not backward compatible with 1.x.

  • Installation
    • Using Gemfile
    • Using command line
  • Usage
    • Configuration
    • Uploadcare File Uploader
      • Widget
        • Using CDN
        • Using NPM
      • Input
    • Using the File Uploader with Rails models
      • Form data
      • File and Group wrappers
    • Image Transformation
    • Uploadcare API interfaces
      • Upload Api
      • File Api
      • Group Api
      • Project Api
      • Webhook Api
      • Conversion Api
  • Useful links

Installation

Using Gemfile

Add this line to your application's Gemfile:

gem "uploadcare-rails"

And then execute:

$ bundle install

Using command line

$ gem install uploadcare-rails

Usage

Configuration

To start using Uploadcare API you just need to set your API keys (public key and secret key). These keys can be set as ENV variables using the export directive:

$ export UPLOADCARE_PUBLIC_KEY=demopublickey
$ export UPLOADCARE_SECRET_KEY=demoprivatekey

Or you can use popular gems like dotenv-rails for setting ENV variables. You must set the gem before uploadcare-rails like this :

gem "dotenv-rails", require: "dotenv/rails-now", groups: [:development, :test]
gem "uploadcare-rails"

:warning: require: "dotenv/rails-now" is very important!

Run the config generator command to generate a configuration file:

$ rails g uploadcare_config

The generator will create a new file in config/initializers/uploadcare.rb.

The public key must be specified in config/initializers/uploadcare.rb to use Uploadcare file upload. This step is done automatically in the initializer if you set the ENV variable UPLOADCARE_PUBLIC_KEY earlier.

...
Uploadcare::Rails.configure do |config|
  # Sets your Uploadcare public key.
  config.public_key = ENV.fetch("UPLOADCARE_PUBLIC_KEY", "demopublickey")
  ...
end

There are also some options set by default:

...
# Deletes files from Uploadcare servers after object destroy.
config.delete_files_after_destroy = true

# Sets caching for Uploadcare files
config.cache_files = true

# Available locales currently are:
# ar az ca cs da de el en es et fr he it ja ko lv nb nl pl pt ro ru sk sr sv tr uk vi zhTW zh
config.locale = "en"

# If true, inputs on your page are initialized automatically, see the article for details -
# https://uploadcare.com/docs/file-uploader-api/widget-initialization/
config.live = true

# If true, input initialization is invoked manually.
# See https://uploadcare.com/docs/file-uploader-api/widget-initialization/).
config.manual_start = false

Then you can configure all global variables such as files storing/caching, deleting files, etc. Full list of available options is listed in the file itself. Just uncomment an option and set the value.

Uploadcare File Uploader

Widget

Using CDN

The fastest way to start using file uploading is to add the Uploadcare widget to the html-page. There is a view helper that can do it with two strings of code:

Add this string to your

html-tag
<!DOCTYPE html>
<html>
<head>
  <title>RailsApp</title>
  <%= uploadcare_widget_tag %>
  <!--
    results in:
    <script src="https://ucarecdn.com/libs/widget/3.x/uploadcare.full.min.js"></script>
    <script>
      //<![CDATA[
      UPLOADCARE_PUBLIC_KEY = "your_public_key";
      UPLOADCARE_LOCALE = "en";
      UPLOADCARE_LIVE = true;
      UPLOADCARE_MANUAL_START = false;
      //]]>
    </script>
  -->
</head>
...

This helper uses a CDN-url for the widget bundle and supports three options:

  • version — version of the Uploadcare widget. Default is "3.x".
  • bundle — bundle name. Available names are "full", "default", "api", "ie8" and "lang.en". Default bundle is "full" — a full bundle with built-in jQuery. More info about bundles here.
  • min — bool value detecting if the bundle must be minified.

The

tag then also includes the