Skip to content

Instantly share code, notes, and snippets.

@harrisonmalone
Last active July 20, 2020 05:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harrisonmalone/f0f95aaeec83c723de211ecffbace846 to your computer and use it in GitHub Desktop.
Save harrisonmalone/f0f95aaeec83c723de211ecffbace846 to your computer and use it in GitHub Desktop.

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:

  1. from the client side you need to change your form to encType="multipart/form-data" to allow for the form to deal with files
  2. you add the file to state via an onChange handler
  3. when the form is submitted you create a form data object that is sent in the body of the POST
  4. all of this code can be found here https://github.com/CoderAcademy-MEL/ft-bookmarks-client/blob/master/src/components/CreateBookmark.jsx
  5. the create action then receives the params and creates the bookmark
  6. rails has active storage tables setup and a bookmark has_one_attached image
  7. rails also has s3 credentials setup and connected to a bucket, the same as what we did with rails/s3 in term 2
  8. the create action then sends back the newly created bookmark as well as the url_for the bookmark.image
  9. 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
  10. you can find all of that code here https://github.com/CoderAcademy-MEL/ft-bookmarks-api/blob/master/app/controllers/bookmarks_controller.rb
  11. 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment