Rails4 Paperclip attachments not working in production - ruby-on-rails

I'm trying to upload Avatars to Users and it's working in development but not in production
Rails4, Heroku, S3, Paperclip, Devise
Everything is working fine in dev. When I push to Heroku, run Heroku migrate , and try to update devise User page with an attachment(:avatar) image, it returns the image url as missing.
In heroku logs --tail it appears to run normally, even saving the image to S3 (I checked and they are saving in my S3 bucket). Also if I pull pg database I can see the columns in the database associated with attachment images (4 columns). In production the attachment file columns are empty when I check the database, so nothing is going to them, but they are stored in S3.
This is kind of weird? I thought Paperclip Docs say that it will save to database and run validations before sending to server.
My Form
My view (index.html.erb)
<%= image_tag post.user.avatar.url(:thumb) %>
And here's my log after successfully saving to S3 (heroku logs --tail)
If I check the database, the columns for attachments are still nil
what confuses me is that this works in Development but not in production and appears to not have anything to do specifically with an S3 error.
what am I doing wrong??

Well, big time-waster.
I finally got so fed up trying to figure this one out I ended up going through all the motions for a 2nd and 3rd time and for whatever reason, it works now :\
-I added paperclip.rb (empty file) in config/initializers/
-I merged all my safe branches into master.
-Ran bundle install
-Pushed to github
-Ran Git push heroku master
-Ran heroku run rake db:migrate
This is the 3rd time running through those operations so I honestly do not know where the problem was but it is working now.
Thanks #peterept for helping

You form should have enctype="multipart/form-data" and the new version of paperclip force you to have this validation:
validates_attachment_content_type :avatar, :content_type => /\Aimage/.*\Z/
Take a look at their documentation: https://github.com/thoughtbot/paperclip

Related

Remote Server behaving weirdly

I am trying to upload multiple images using paperclip gem. I have controller BatchImagesController, an associate batch images model and an attachment model image_assets. Now the issue is, my app is working fine on localhost in development and production environment. But on Remote server its not uploading images. The database shows batch_images entries but nothing uploads in image_assets. I am unable to debug here. Production log is also not showing anything. I am using ruby 1.9.3 and rails 3.2.13.
Any suggestion what I might be doing wrong?? Is this a mass assignment issue??I have made the
config.active_record.whitelist_attributes = false

File upload to Heroku not working [duplicate]

This question already has answers here:
Heroku file upload problem
(5 answers)
Closed 8 years ago.
I've got ROR application deployed on Heroku but the file upload doesn't seem to be working at all.
I do have multipart => true set up on the forms.
This is definitely working on my localhost environment.
Is there something that I need to enable to get this working?
Heroku doesn't accept file uploads the last I checked. You'll need an Amazon S3 or something similar to accept the files. If you upload a file to Heroku, you can access it for the duration of the request via the Tempfile class, but it will not get saved.
https://devcenter.heroku.com/articles/paperclip-s3
https://devcenter.heroku.com/articles/s3
This error trips up a lot of people when they start using Heroku, me included!
The reason that you can't store files locally on Heroku is that this just isn't how Heroku works. Heroku takes a copy of your git repository and bundles it up into a "slug" which then gets run on their servers. Anything outside your slug (i.e. that's not stored within your git repo) will be lost when the dyno (virtual UNIX instance) restarts.
You can see this by firing up a console with heroku run rails c and creating and saving a new file using Ruby's File object. The new file will save correctly, and you can do things like require it or read from it, but if you close and reopen the console window, the file will have disappeared.
So if you want to store files that are being uploaded through a form, you need to use an external storage service. I like Amazon S3 as it's very simple to integrate with Heroku using Paperclip, as the links in the other answer mention.

Heroku: Is there a way to migrate database records made in development to Heroku?

Hi I'm currently using carrierwave on my rails app without Amazon S3 or any storage, so the pictures I upload onto my heroku app keep disappearing. I was wondering if there was a way to migrate existing records beforehand(made in development mode) to heroku so that those pics would be stored forever in the local filesystem instead of disappearing?
If you only want to have some pics displaying, you can eventually manually add them in your public folder, at least they will display properly.
For example, you can add the picture "/public/uploads/recipe/photo/5/chicken-wings.jpg" to your git repo and push it to heroku.
Of course this is not the perfect solution as you add this image into your git repo which is not what you want when you will launch the application in production but as Heroku is a read-only file system this is the only way to store file on their filesystem.
You'll need the taps gem
gem install taps
Then do:
heroku db:push [options]
use --tables [array of tables you want pushed] if you need to upload specific tables only.
source

Newly uploaded images break in production but not in development

I would appreciate any feedback regarding what may be causing my issue, described below.
I have an application that allows users to upload images. Everything works fine in development and used to work fine in production.
Recently, my newer images have all broken. I can upload new images, but when I check back a few hours later, the images are broken again. This started happening about a week ago, and images that I've had up in production from before then are still ok.
I am using Rails with Bootstrap and SimpleForm, and using Paperclip for the images. I am using Postgres in both devleopment and production, and am deploying to Heroku.
The only hint I have is in the "blank_profile_pic.png" image that I use as a default when users don't have a profile picture uploaded.
User.all.each do |u|
if u.profile_pic.file?
image_tag(user.profile_pic)
else
image_tag("blank_profile_pic.png")
end
end
For users that don't have a profile_pic uploaded, a broken image appears if their profile was created in the last week, but the expected "blank_profile_pic.png" remains for people that created their account before the issues started surfacing a week ago. How can the same block of code return different results between recent and older users?
I really don't know where to start with this, so would appreciate any feedback regarding what possible causes could be, and if there are any other files that I can show here.
Thanks very much for your help!
Heroku is a read only system. So you most definitely have to upload your images either to S3 or some other cloud provided.
Your images might have been uploaded to /tmp in Heroku and then somehow it was cleared, hence the errors.
Here are the docs: https://devcenter.heroku.com/articles/read-only-filesystem
And configuring Paperclip with S3: https://github.com/thoughtbot/paperclip#storage
I am not sure if you have the imagemagick gem installed, but we had same issue with image_magic that was breaking our paperclip functionality in production, but not in development (weird, I know). Yet even after removing imagemagick from our gemfile and Gemfile.lock locally (running bundle install and all that stuff) and then deploying back to production on heroku, the error persisted in production! (weird, I know).
What ended up doing the trick was running:
$ heroku repo:purge_cache -a myAppName
(Taken from: https://github.com/heroku/heroku-repo#purge_cache)
When you deploy your app, Heroku caches some things like your assets and installed gems in order to speed up deployment. Although this is a great feature, it can have side-effects sometimes, and in this case, it seems that something about the imagemagick gem got "stuck" in production's cache, which is why purging solved the issue for us (since after purging, your app will rebuild itself from scratch on your next deployment)

Copy Photos to Staging S3 (using rails and paperclip)

I've got a website built on rails that uses paperclip to persist photos to S3. This works great. I'm using Heroku, so i've also got a staging server set up for future changes and user testing. Following the 12factor Dev/Prod parity http://www.12factor.net/dev-prod-parity in mind I've been using PGbackups to keep the same data in staging as in production. However I can't find a good way to sync my production S3 bucket to staging.
I don't want to use my production S3 bucket in staging for fear of accidentally deleting production photos. Having missing photos show up durring user testing is distracting and unacceptable. Is there an easy solution to push the same photo to two buckets while saving, or to set staging to be a "slave" to the production bucket in S3?
You can run a script to copy your production bucket to staging whevener you synchronize your database: Googling a bit I've found this one. You can wrap it in a rake task and run it from your staging app.
The most recent best answer is on a similar question:
Best way to move files between S3 buckets?
Scroll down until you see the answer by alberge

Resources