Ruby on Rails with active_storage

File uploading in Ruby on Rails application with Active Storage gem using ImageKit.io.

This is a quick start guide to show you how to integrate ImageKit in the Ruby on rails application with active_storage gem. The code samples covered here are hosted on Github - https://github.com/imagekit-samples/quickstart/tree/master/ruby/sample-app-with-activestorage.

This guide walks you through the following topics:

Setting up ImageKit Ruby on Rails SDK

We will create a new rails application for this tutorial and work with it.

If you already have an existing Rails app, you would need to modify the terminal commands and configurations in this tutorial as applicable.

Let's create a new rails application. Create a new directory and enter the command:

rails new sample-app-with-activestorage

Now, navigate to the newly created directory, and run the app:

rails server

In your web browser, navigate to http://localhost:3000/

You should see a generic welcome message (Yay! You're on Rails!).

Next, to use ImageKit functionality in our app, we will create a new controller with the following command:

rails generate controller Welcome

This will generate a few scaffolding files for you. Go to config/routes.rb and add the following lines:

get 'welcome/index'
root 'welcome#index'

Here, we are doing two things:

  1. One, creating a route at the path _welcome/index, _to direct requests coming on that path to go to the index action of the Welcome controller, which we will add soon.

  2. Second, we are declaring the root path to be directed to _welcome/index. _This means that all requests on http://localhost:3000/ will go to http://localhost:3000/welcome/index

Installing the SDK

First, add this line to your Gemfile to add the 'imagekitio' gem as a dependency.

gem 'imagekitio'

Next, run the bundle install command to install the imagekitio gem from the RubyGems repository.

bundle install

Initializing the SDK

Before the SDK can be used, let's learn about and configure the requisite authentication parameters that need to be provided to the SDK.

Create a file config/initializers/imagekitio.rb, open it and add your public and private API keys, as well as the URL Endpoint as follows: (You can find these keys in the Developer section of your ImageKit Dashboard)

ImageKitIo.configure do |config|
  if Rails.env.development?
    config.public_key = 'your_public_key'
    config.private_key = 'your_private_key'
    config.url_endpoint = 'your_url_endpoint' # https://ik.imagekit.io/your_imagekit_id
  end
  config.service = :active_storage
  # config.constants.MISSING_PRIVATE_KEY = 'custom error message'
end
#make sure to replace the your_public_key, your_private_key and your_url_endpoint with actual values

Restart the application

rails server

The home screen will display an error message stating that the index action could not be found for the WelcomeController. Let's fix that. Open up the project in your text editor of choice, and navigate to app/controllers/welcome_controller.rb. Edit it to make it look like the following:

class WelcomeController < ApplicationController
  def index
  end
end

and create app/views/welcome/index.html.erb file and edit file with text Hello world. Now refresh the browser it renders the Hello world.

Uploading images in Ruby on Rails application

Attaching the image to a model (using active_storage)

You can attach an image as an attribute of one of your models, making it convenient to write a model-oriented code. This method uses the ActiveStorage gem, which is a dependency of the imagekitio gem. Note: From Rails version 5, rails support active_storage by default.

rails active_storage:install
rails db:migrate

Let's add imagekit service on config/storage.yml as below:

imagekitio:
    service: ImageKitIo

and on config/environments/development.rb file:

config.active_storage.service = :imagekitio

Now, we generate a model and give it a picture attribute, which will be an image. If you already have a model in your project, you can simply add the attribute to the existing model.

rails generate model Post

Now go to the model file - app/models/post.rb, and add the following lines:

class Post < ApplicationRecord
    has_one_attached :picture
end

You are free to change the name of the attribute from :picture to something else.

Perform the database migrations. This will add two columns - title, picture to the Posts table of our database.

rails g migration AddTitleToPosts title:string
rails db:migrate

Now, create another controller named after the model. For example, we'll create a controller called Post.

rails generate controller Posts

Now, go to app/controllers/posts_controller.rb, and add the following methods:

class PostsController < ApplicationController
    def index
        @posts = Post.all
    end
    def show
        @post = Post.find params[:id]
    end

    def new
        @post = Post.new
    end

    def create
        @post = Post.new post_params
        if @post.save
            redirect_to @post
        else
            render 'new'
        end
    end

    def post_params
        params.require(:post).permit(:title, :picture)
    end
end

Here, we create standard methods to show all posts, show a single post, and create a new post. These methods will have to be accompanied by some HTML markup as well. So go to app/views/posts/, and add the following files

index.html.erb

<%= @posts.each do |post| %>
    <div class="post">
        <div class="image">
            <%= image_tag post.picture.url(transformation: [{height: 200, width: 300}]) %>
        </div>
        <div class="content">
            <%= link_to "#{post.title}", post_path(post), class: "header" %>
        </div>
    </div>
<% end %>
<%= link_to 'Back', posts_path %>

new.html.erb

<%= form_with model: @post do |form| %>

    <%= label_tag(:title, "Enter title of post") %>
    <%= form.text_field :title %>
    
    <%= label_tag(:picture, "Upload a picture") %>
    <%= form.file_field :picture %>
    
    <%= submit_tag "Submit" %>
<% end %>

<%= link_to 'Back', posts_path %>

show.html.erb

<div class="post">
    <div class="image">
        <%= image_tag @post.picture.url %>
    </div>
    <div class="content">
        <%= link_to "#{@post.title}", post_path(@post), class: "header" %>
    </div>
</div>
<%= link_to 'Back', posts_path %>

And finally let's add the routes config/routes.rb

resources :posts

Now, restart the server and go to _http://localhost:3000/posts/new. _You should see two form fields, for title, and picture. Enter a text value for the title field, and upload an image/file for the picture field, and click on Submit. This should redirect you to _http://localhost:3000/posts/1, _displaying the title and the image.

This sums up how you can upload images to your Media Library using the SDK. The second method is more suited for RESTful applications with CRUD operations where the image is an attribute of a resource.

Authentication parameter generation

You can use the SDK to generate authentication parameters that are required if you want to implement client-side upload functionality using javascript or some front-end framework.

# app/controllers/welcome_controller.rb

def auth_params
  @imagekitio = ImageKitIo.client
  @auth_params = @imagekitio.get_authentication_parameters()
end
# config/routes.rb

get 'welcome/auth_params'
# app/views/welcome/auth_params.html.erb

<div>
    <%= @auth_params %>
</div>

The get_authentication_parameters(token = nil, expire = nil) takes two optional arguments, token, and expire. token defaults to a random string if not provided. expire defaults to infinite.

You should see an output like this:

{    
    :token=>"6b4d79e5-eb36-4de0-8ef5-534c88e45ebf",
    :expire=>1600957501,
    :signature=>"4c19c4066a140921eddaff7aaa3625df2bf8182a"
}

What's next

The possibilities for image manipulation and optimization with ImageKit are endless. Learn more about it here:

Last updated