Upload image with RoR and Heroku - ruby-on-rails

I recently deployed my ruby on rails app on Heroku and I am using the pg gem for my database.
I created a page where I can upload images and have them show on the main page and they are saved in a public folder within the app.
When I deployed it was working fine, but after sometime the images disappeared. When I looked at my Heroku logs I got the following:
ActionController::RoutingError (No route matches [GET] "/assets/products/6/photos/medium/shanab.jpg"):
I think the images are saved, but I don't know how to check if they are still there.

but after sometime the images disappeared
Heroku runs an ephemeral file system which means that each time you deploy a new version of your code, the old files/code are overwritten:
Each dyno gets its own ephemeral filesystem, with a fresh copy of the most recently deployed code. During the dyno’s lifetime its running processes can use the filesystem as a temporary scratchpad, but no files that are written are visible to processes in any other dyno and any files written will be discarded the moment the dyno is stopped or restarted.
This means that even if your images are working for a time, each new deploy will basically overwrite them. This is probably the cause of the problem, although I can't be sure without further inspection.
--
The way around this is to use a CDN, most common (although incorrect) is to tie your app with S3.
I don't know which asset-processing gem you're using, but if it's Paperclip, you'll want to use the inbuilt S3 functionality to put all your image assets onto Amazon's servers:
#app/models/x.rb
class x < ActiveRecord::Base
has_attached_file :download,
:storage => :s3,
:s3_credentials => Proc.new{|a| a.instance.s3_credentials }
def s3_credentials
{:bucket => "xxx", :access_key_id => "xxx", :secret_access_key => "xxx"}
end
end

Related

loading images from /images not working in production | Ruby on Rails

My rails app was loading images I added previously. The old ones are all loading from /assets (and actually loading) the new images I add are trying to load from /images instead of /assets (and not working)
My rails app is running on Ubuntu 16 with Nginx and passenger.
The way I get them is just by entering the name of the file with .png at the end (ex: picture.png) into a simple_form string input.
And it is rendered from this: <%= image_tag("#{post.thumbnail_link}", :alt => "#{post.title}", :width => 320, :height => 180, class: "news-object") %>
And through this on the post show: <%= image_tag("#{#post.thumbnail_link}", :alt => "#{#post.title}", class: "inpost-thumb") %>
The ones added previously also load with a long string of seemingly random numbers/letters. - The new ones don't.
The only thing I can think that is different is that the new images have been pushed to the server recently, whereas the old images may have been there when I put the rails app onto a production server.
I've tried doing Rails.application.config.assets.precompile += ["*.png"] in config/initializers/assets but that doesn't work.
The images are loading fine in development on localhost.
I want the images to load from /assets like the old ones do.
Both old and new are in /assets/images
You might try running:
RAILS_ENV=production bundle exec rake assets:clobber
RAILS_ENV=production bundle exec rake assets:precompile
Add and commit the changes and run a deploy. Looks like your new assets simply haven't been compiled.
More info on the Rails asset pipeline can be found here: https://edgeguides.rubyonrails.org/asset_pipeline.html
A quick read through there may save you quite a bit of time.

Deploying on Heroku - Images disappears after upload

I deployed an application on Heroku which I wrote on Ruby on Rails. It is a movie review app.
I am able to upload images from my computer, to the web app online. Everything else works as per my expectations.
The images disappear after a day. My requirement is to have the image continue to render.
I am using Paperclip gem from rails. This only happens on the deployed version and not on localhost.
The default upload location on Heroku is into temporary storage. This is because you will get a different web worker each time you deploy.
You need to use S3 or another location to store your files. Luckily this is well documented for Paperclip on Heroku.
The main configuration difference is this. Add gem 'aws-sdk' to your Gemfile and then adjust your config file in config/environments/production.rb:
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['S3_BUCKET_NAME'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}

Rails 4.1.0/Paperclip/nginx send_file not working with large PDFs

I have a Rails 4.1.0 app running with nginx, hosted on Engineyard. I'm having some puzzling issues with the send_file method (also tried send_data) with pdf files.
The setup:
In my controller:
send_file path, type: asset_content_type, disposition: 'inline', filename: filename
I have confirmed with logger that all the variables in the above line are correct. I uncommented this line in the config for the environment in question:
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
Here's the paperclip setup on the model:
class Asset < ActiveRecord::Base
belongs_to :attachable, polymorphic: true
has_attached_file :asset,
styles: lambda { |i| i.instance.attachable_styles },
path: ':rails_root/private/system/:attachment/:id_partition/:style/:filename',
url: '/files/:id/disposition/:style/:hash.:extension',
preserve_files: true
validates_attachment_content_type :asset, :content_type => /.*/
The symptoms:
No problems at all locally running POW or webrick.
With the app running remotely, downloading any kind of image file of any size works normally.
Downloading pdfs ~ 140KB or less works normally.
Downloading pdfs larger than that, I get a fraction of the file. Say I have a 1MB pdf. I download it, and end up with a corrupted file that is around 140KB.
I have confirmed that file on disk is fine. I can scp the same large pdf to my local machine, and the pdf is fine.
The paperclip record in the db is normal.
Maybe it's an nginx config issue I'm overlooking?
EDIT: The pdf thing is a red herring. It appears all large files are only partially sent.
EY support gave me a hand. Here are the steps they followed to solve the issue:
Noted the directories that nginx was referencing did not seem to match up with what was in /var/tmp/nginx/proxy.
Removed all the temporary folders in /var/log/nginx/proxy, and restarted nginx.
Now it works.

file duplication using asset_sync gem on rails 4

I'm having some problems with assets on Heroku (rails) and am hoping someone can point me in the right direction. I've got the asset_sync gem installed, and after many hours of debugging I've finally got it working. However, when I first run (with an empty S3 bucket) "git push heroku master", I get about 4 copies of every file uploaded to s3 (each with a different hash appended). Also, somehow a lot of files I previously deleted (and are no longer in my app/assets/images directory) are still somehow getting uploaded. I've deleted the public/assets folder on my local copy & pushed to git, but perhaps that folder is still there on heroku? How do I debug this? I want my assets to be properly sync'd, so if I delete an image while developing locally, it will also be removed from s3 when I next deploy.
Another possibly related problem, my static error pages (public/404.html) are not getting served on heroku, yet work fine on development- are these static html files treated as assets and meant to be uploaded to S3 too?
Running heroku run rake assets:precompile does nothing. My asset_sync.rb initializer is:
if defined?(AssetSync)
AssetSync.configure do |config|
config.fog_provider = 'AWS'
config.aws_access_key_id = 'key'
config.aws_secret_access_key = 'key'
config.fog_directory = 'bucketname'
config.fog_region = 'us-east-1'
config.existing_remote_files = "delete"
end
end
I know I should be using environment variables but it shoudln't make any difference hardcoding my access details at least while I'm testing
Thanks for any help.

Routing error for images Ror

I run my app locally on 3000 and all is fine, but when i upload to heroku it is sub standard.
I am using paperclip gem,
my Heroku logs issues this error
ActionController::RoutingError (No route matches [GET] "/images/medium/missing.png"):,
There seems to be no path to this instance in my pipeline, If it runs locally and not in heroku i figure it's a Heroku config problem?.
For heroku you have to additionally use the aws-sdk gem like shorlty described in the excellent paperclip-intro.
Sample configuration:
# config/environments/production.rb
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['S3_BUCKET_NAME'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
Best info point is usually heroku itself, see this example configuration guide
paperclip gem means you are uploading images, and want to access the image via that GET request right?
that path looks like it is for the heroku server itself. Heroku doesn't support file uploads, get amazon s3 setup or some other storage facility.
it works locally because your local setup allows for file uploads and storage.

Resources