Using FormData Objects
Recently, while building a React-Redux and Ruby on Rails backend, I came across an issue with regards to Active Storage photo uploads. While having used Active Storage in the past with Ruby frontend and Rails backend was relatively simple due to the form view, implementing this on the React side took a little more research!
After setting up Active Storage on the backend, I came across FormData objects! These objects let you compile a set of key/value pairs to send using XMLHttpRequest and is intended for sending form data. This is exactly what I needed for the submission of my form, which included the picture upload.
Get Sarah Rivas’s stories in your inbox
Join Medium for free to get updates from this writer.
Let’s take a look at how to build your FormData objects yourself:
In the code snippet above, we set a new variable equal to new FormData(),which creates the FormData object. Once the object has been created, you can append the form fields individually. Once all form fields have been appended to data, it will be passed along as the body within our fetch request, as you can see on line 14.





