ruby

Why Should Shrine Be Your Go-To Tool for File Uploads in Rails?

Revolutionizing File Uploads in Rails with Shrine's Magic

Why Should Shrine Be Your Go-To Tool for File Uploads in Rails?

Easy File Uploads with Shrine in Rails

When it comes to handling file uploads in Ruby on Rails, there are quite a few options on the table. However, one gem that stands out for its sheer flexibility and modularity is Shrine. Shrine is a robust toolkit designed to deal with file attachments in Ruby apps, and it fits into Rails like a glove. Here’s a goto-guide to get you on your way to setting up Shrine for file uploads in your Rails project.

First off, get Shrine up and running in your Rails app. If you don’t already have a Rails app, create a new one.

rails new photo_app -T

The -T flag will skip the default unit test setup.

Now, add the necessary gems to your Gemfile:

gem 'shrine', '~> 3.3'
gem 'image_processing', '~> 1.12', '>= 1.12.1'

The image_processing gem comes in handy for those times when you need to create different image sizes or perform image type and size validations. Run bundle install to set them up.

Now, Shrine needs some initial configuration. Head over to the config/initializers directory in your app and create a file named shrine.rb. Throw in the following:

require "shrine"
require "shrine/storage/file_system"
require "shrine/storage/memory"

if Rails.env.test?
  Shrine.storages = {
    cache: Shrine::Storage::Memory.new,
    store: Shrine::Storage::Memory.new,
  }
else
  Shrine.storages = {
    cache: Shrine::Storage::FileSystem.new("public", prefix: "uploads/cache"),
    store: Shrine::Storage::FileSystem.new("public", prefix: "uploads"),
  }
end

Shrine.plugin :activerecord 
Shrine.plugin :cached_attachment_data 
Shrine.plugin :restore_cached_data 
Shrine.plugin :validation_helpers
Shrine.plugin :validation

This sets up the different storage options for both cached and permanent files. You can always switch to cloud storage like Amazon S3 for production, but file system storage works fine for now.

Next, let’s make your model and create a migration to include a column for storing file data. Generating a model for Photo would look something like this:

rails generate model Photo title:string image_data:text
rails db:migrate

This will create a photos table with an image_data column where Shrine will stash all the info about the uploaded file.

Right, so now we create an uploader class to actually handle the file uploads. In the app/uploaders directory, make an ImageUploader class:

class ImageUploader < Shrine
  # plugins and uploading logic
end

Include this in your model:

class Photo < ActiveRecord::Base
  include ImageUploader::Attachment(:image) # adds an `image` virtual attribute
end

To get your users uploading files, you’ll need form fields in your views. Here’s an example of what that might look like:

<%= form_for @photo do |f| %>
  <%= f.hidden_field :image, value: @photo.cached_image_data, id: nil %>
  <%= f.file_field :image %>
  <%= f.submit %>
<% end %>

Handling the uploads in your controller is straightforward, too. Here’s how you can manage that in the PhotosController:

class PhotosController < ApplicationController
  def create
    @photo = Photo.new(photo_params)
    if @photo.save
      redirect_to @photo, notice: 'Photo was successfully created.'
    else
      render :new
    end
  end

  private

  def photo_params
    params.require(:photo).permit(:title, :image)
  end
end

One of the coolest features of Shrine is handling direct uploads and background processing.

Direct uploads let files go directly from the client to your storage asynchronously, making for a slick user experience. Using the upload_endpoint plugin sets this up nicely:

Shrine.plugin :upload_endpoint

# config/routes.rb
Rails.application.routes.draw do
  mount Shrine.upload_endpoint(:cache) => "/upload" # POST /upload
end

You can then configure a JavaScript library like Uppy to make use of this endpoint.

For background processing, particularly useful for generating thumbnails or handling big files, you can leverage libraries like Sidekiq or Resque. Here’s how you might use Sidekiq for background processing:

class Photo < ActiveRecord::Base
  include ImageUploader::Attachment(:image)

  after_save :process_image_in_background

  private

  def process_image_in_background
    ImageProcessingJob.perform_later(self)
  end
end

class ImageProcessingJob < Sidekiq::Worker
  sidekiq_options retry: false

  def perform(photo)
    photo.image_derivatives!  # Process the image here
  end
end

While on the topic of storage, Shrine also supports cloud storage services like Amazon S3, Google Cloud Storage, and Cloudinary.

For Amazon S3, you need to add some gems and configure the storage options like so:

require "shrine/storage/s3"

s3_options = {
  access_key_id: ENV['S3_KEY'],
  secret_access_key: ENV['S3_SECRET'],
  region: ENV['S3_REGION'],
  bucket: ENV['S3_BUCKET'],
}

Shrine.storages = {
  cache: Shrine::Storage::FileSystem.new("public", prefix: "uploads/cache"),
  store: Shrine::Storage::S3.new(prefix: "store", **s3_options),
}

This sets up your permanent storage to use Amazon S3.

In a nutshell, Shrine is a great gem for dealing with file uploads in Rails apps. Its modular nature, cloud storage support, and nifty features like direct uploads and background processing make it a go-to for managing file attachments. By following these steps, you’ll have Shrine up and running in your Rails app in no time. Whether you’re working with images, videos, or any other files, Shrine gives you the tools you need to handle them smoothly.

Keywords: Shrine, file uploads, Ruby on Rails, file attachments, image processing, cloud storage, Amazon S3, background processing, direct uploads, Uppy



Similar Posts
Blog Image
7 Proven Techniques for Building Advanced Search in Rails Applications

Discover 7 advanced techniques for building powerful search interfaces in Rails applications. Learn full-text search, faceted filtering, typeahead suggestions, and more to enhance user experience and boost engagement in your app. #RubyOnRails #SearchDevelopment

Blog Image
Should You Use a Ruby Struct or a Custom Class for Your Next Project?

Struct vs. Class in Ruby: Picking Your Ideal Data Sidekick

Blog Image
Mastering Rails API: Build Powerful, Efficient Backends for Modern Apps

Ruby on Rails API-only apps: streamlined for mobile/frontend. Use --api flag, versioning, JWT auth, rate limiting, serialization, error handling, testing, documentation, caching, and background jobs for robust, performant APIs.

Blog Image
10 Advanced Ruby on Rails Strategies for Building Scalable Marketplaces

Discover 10 advanced Ruby on Rails techniques for building scalable marketplace platforms. Learn about multi-user management, efficient listings, and robust transactions. Improve your Rails skills now.

Blog Image
12 Powerful Techniques for Building High-Performance Ruby on Rails APIs

Discover 12 powerful strategies to create high-performance APIs with Ruby on Rails. Learn efficient design, caching, and optimization techniques to boost your API's speed and scalability. Improve your development skills now.

Blog Image
Ruby Performance Profiling: Production-Ready Techniques for Identifying Application Bottlenecks

Discover proven Ruby profiling techniques for production apps. Learn execution, memory, GC, and database profiling to identify bottlenecks and optimize performance. Get actionable insights now.