Skip to content

Instantly share code, notes, and snippets.

# app/controllers/things_controller.rb
class ItemsController < ApplicationController
def create
thing = Thing.new(thing_params)
if thing.create
render json: thing, status: :created
else
render json: thing, status: :unprocessable_entity
end
import { DirectUpload } from 'activestorage'; // https://www.npmjs.com/package/@rails/activestorage
createThing = (name, the_attachment) => { // name is a string, the_attachment is a File object
const upload = new DirectUpload(
the_attachment,
'/rails/active_storage/direct_uploads', // This url is exposed by default in your app
);
upload.create((error, blob) => {
if (error) {
// Something happened, handle the error
@camiloforero
camiloforero / routes.rb
Created September 30, 2019 13:34
ActiveStorage routes
Rails.application.routes.draw do
scope ActiveStorage.routes_prefix do
get "/blobs/:signed_id/*filename" => "active_storage/blobs#show", as: :rails_service_blob
get "/representations/:signed_blob_id/:variation_key/*filename" => "active_storage/representations#show", as: :rails_blob_representation
get "/disk/:encoded_key/*filename" => "active_storage/disk#show", as: :rails_disk_service
put "/disk/:encoded_token" => "active_storage/disk#update", as: :update_rails_disk_service