Send a Cropped Image From React to Rails For Attachment and Storage.

Adam Workman
Apr 3 · 4 min read

So, you want to save an altered image for later use. Let’s say you want to allow the user to upload a photo, crop it, and save that cropped image as their avatar. Here I am going to go over one solution that I have found for getting the altered image information from React to Rails and attaching that information to the user via ActiveStorage. I will not be covering how to upload a photo, crop it, or how to set up ActiveStorage. For that information, I recommend that you check out the links below.

~upload/ActiveStorage/cloudinary ~crop~

Now the fun stuff! I am using a handy component called react-easy-crop to crop my images (see above link). It comes with the callback onCropComplete which is passed 2 arguments croppedArea and croppedAreaPixels, and is called when the user stops moving the media or stops zooming. This seems like the logical place to start.

Basically, what we want to do here is get the cropped image and set that information to a state variable. Nice and easy! To do this, we are going to want to create and image from only the cropped section of the image (we will use the information we get from onCropComplete), then return that image as data. The cropImage component is built to do just that.

Here we create a ‘canvas’ element and use .useContext(‘2d’) to create a 2d shape on it. We use the information we passed in from onCropComplete (croppedAreaPixles) to draw only the cropped section of the image on our canvas using .drawImage(). Then we return the canvas as base64. All of this is done using promise-based functions to suspend execution until the returned promise is fulfilled or rejected. This is to make sure the functions have the information they need before moving on. Once we receive the image data from getCroppedImg, we are going to save that information to a state variable. Now, we need to get that information to our Rails back end.

One way to get the image information where it needs to go, is through the existing form where we uploaded the original file. So, in the existing form, we will add an input with the value equal to the state variable that contains the information for the altered image. The ‘name’ of this input will be very important. You will want the name of the input to be the same as the attachment in ActiveStorage… we will circle back to this.

The submit handler is pretty straight forward. We just instantiate a new FormData object and pass in our form, then send that information in the body of a fetch request.

Once that information arrives at our Rails API, we need to attach it to the appropriate user. First lets add some functionality to ActiveStorage. Let’s add gem ‘active_storage_base64’ to our GemFile. Then we will want to add include ActiveStorageSupport::SupportForBase64 to application_record.rb. Now we can attach the base64 image to our user!

To attach the image to the user, just add has_one_base64_attched :foo. Make sure the name of the input that stores the image data in your form and the name of has_one_base64_attached in your model are the same! This is because has_one_base64_attached is going to look for, in this case, a param named ‘profile_img’ to attach to the user. If there is nothing coming in with that name, then it will not attach anything.

Depending on how you have ActiveRecord set up, that’s it! The images that are being uploaded and cropped in the front end, are now being attached to the appropriate user. Happy coding!

Geek Culture

Proud to geek out.

Sign up for Geek Culture Hits

By Geek Culture

Subscribe to receive top 10 most read stories of Geek Culture — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Geek Culture

A new tech publication by Start it up (https://medium.com/swlh).

Adam Workman

Written by

Geek Culture

A new tech publication by Start it up (https://medium.com/swlh).

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store