Upload Binary Files: Images, Attachments, Etc
Jets supports the ability to upload files like images via Binary Support. Binary support is enabled for
multipart/form-data data. Jets converts the uploaded data from a standard HTML file input field and passes it to your controller as a file object through the
Here’s an example form with an HTML file input field:
<%= form_tag(action, multipart: true) do %> <div class="field"> <%= label_tag :photo %> <%= file_field_tag "post[photo]" %> </div> ...
NOTE: It is important to have
multipart: true and also remove any
<input type="hidden" name="_method" value="put" /> from the form to ensure the data gets passed from the form correctly to API Gateway.
When the user submits the form, the controller will receive a
params containing the file upload as an File like object. Here’s an example
You can use
params[:post][:photo] in the controller to read the file and save it to whenever you need, say s3.
This blog tutorial provides an example of image uploading with Jets and Carrierwave: Image Upload Carrierwave Tutorial.
Pro tip: Use the <- and -> arrow keys to move back and forward.