i have an image uploads implementation finished for my bookmarks app https://harrisonsbookmarks.netlify.app
i can write full docs for it as well but here is a quick summary:
- from the client side you need to change your form to encType="multipart/form-data" to allow for the form to deal with files
- you add the file to state via an
onChange
handler - when the form is submitted you create a form data object that is sent in the body of the POST
- all of this code can be found here https://github.com/CoderAcademy-MEL/ft-bookmarks-client/blob/master/src/components/CreateBookmark.jsx
- the create action then receives the params and creates the bookmark
- rails has active storage tables setup and a bookmark has_one_attached image
- rails also has s3 credentials setup and connected to a bucket, the same as what we did with rails/s3 in term 2
- the create action then sends back the newly created bookmark as well as the url_for the
bookmark.image
- for the index action we have a custom method
generate_image_urls
which accesses all the image bookmarks and attaches them to each individual bookmark via a map - you can find all of that code here https://github.com/CoderAcademy-MEL/ft-bookmarks-api/blob/master/app/controllers/bookmarks_controller.rb
- there is a more elegant solution to step 9 using jbuilder which ed intends to show you as an extra class maybe next week, for now this is okay