My application is using the following gems:
gem "refile", require: "refile/rails"
gem "refile-s3"
gem "refile-mini_magick"
What my application does is that it uploads image files to an AWS S3 instance, then gets retrieved using helper methods from the Refile gem.
Everything works as expected when I run it locally.
The problem is when I push my app to Heroku, I can upload images and it displays correctly. However, when I push a newer commit to Heroku, all previous images' links break.
I've checked that the file_id of the image on the page is the same file_id of the image on the Heroku database.
This is my upload form for my images:
<div class="field">
<%= f.label :images_files %><br>
<%= f.attachment_field :images_files, multiple: true, presigned: true, direct: true %>
</div>
This is a snippet of my image being shown:
<%= attachment_image_tag(project.images.first, :file, :fill, 750, 400) %>
I have set my environment variable correctly on Heroku so that it knows the keys to my AWS instance (access key id, secret access key, and bucket name).
This is the accessor to upload to S3:
# config/initializers/refile.rb
require "refile/s3"
aws = {
access_key_id: ENV['AWS_ACCESS_KEY_ID'],
secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
region: "us-west-1",
bucket: ENV['export S3_BUCKET'],
}
Refile.cache = Refile::S3.new(prefix: "cache", **aws)
Refile.store = Refile::S3.new(prefix: "store", **aws)
Another thing I've noticed was that my S3 bucket didn't hold the images in its correct folder if I uploaded through my Heroku app. However, if I uploaded an image using the local server I can see the image in my S3 bucket. I'm not sure why because all the environment variables are correct and I've double-checked it.
Here's a snippet of the Heroku log trying to load a picture:
2016-01-12T23:04:39.448906+00:00 app[web.1]: Refile::App: Could not find attachment by id: d8fffa94c41f9f0ae1b233d69a75787a9a29340a9a83a831385d84d53ad8
2016-01-12T23:04:39.442789+00:00 heroku[router]: at=info method=GET path="/attachments/8718f3743b8d7c91e296ac5f0e6ea324d1fd2868/store/fit/800/475/aee08fe8f97269
826c51a77a11995c77467e24aa3581563d6f298e0dbf9b/file" host=www.domainname.com request_id=49edc34f-2d37-4ae4-9a58-e6265e00032e fwd="99.100.27.10" dyno=web.1 connect
=1ms service=9ms status=404 bytes=424
I've made a stupid mistake when I first started developing this app. In my .gitignore, I put the rule "/config/*" so it didn't upload my accessor to Heroku.
I've then deleted that rule and put stricter rules to just ignore sensitive files containing keys i.e. "/config/*.yml"
It works now, I hope someone can learn from my terrible mistake.