Shrine for File Uploads
I’m working through how to do file uploading, and it seems like Shrine gem is a great way to add this feature.
It’s a bit complex and I’ll have to learn how to use it but luckily, GoRails has a lot of material on in.
Here are the Srine docs (shrinerb.com)
The design architecture follows:
Assuming we want the attributes to be called ‘photo’
- Having a table with a text column (photo_data) for json serialized data
- Making an
ImageUploader < Shrineclass
- Adding this to a model through
user = User.create(photo: File.open('path/to/photo.jpg'))
user.photo.id # 'user/532/photo/f42343fasd.jpg'
Getting it into my app
- Set up locally
- Set up through amazon s3
- go ahead and deploy
I’ll just set it up locally first and then worry about amazon later. GoRails does a great job of breaking it up like this.
Gotcha’s for the form implementation
- Quick tip (@photo can be reference through the
fparameter of a form as
- Be sure to add the
f.hidden_field :photo, value: f.object.chached_photo_databefore the
Setting up the configurations with s3
Shrine also allows for front end file uploading and there’s great videos through GoRails. I’m thinking I’ll just do the simple straight set up and add the front end stuff later if I have time.
There are chache and store directories
Implementation mental models
Shrine first stores the image in a
/cache folder until the object becomes
permanent, at which time it will store another copy of the image in