Precompiling uploaded assets - ruby-on-rails

I have a rails 3.1 application where users upload pictures. I am storing them in /assets/images since that is the path image_tag looks for instead of public/images.
Everything works fine in development but I deployed to Heroku and it gives me this error:
ActionView::Template::Error (image_name.jpeg isn't precompiled)
What is the right way to handle such a situation? Is there a way to compile images after uploading or should I store them somewhere else?

You must not use the filesystem on Heroku to store uploads.
You should not use image_path with uploaded images, since that assumes it is looking at the filesystem. If you use image_tag, you must pass a complete URL, not just an image name.

Are you using carrierwave for your images uploads? You can store them on amazon S3 reasonably easy with carrier wave. Carrierwave instructions Other solutions have S3 storage easily accessible as well.
Heroku will NOT let you store files in the filesystem. Run
RAILS_ENV=production bundle exec rake assets:precompile
to compile you assets locally, add to git, and push to heroku but you cannot add images later via your application on heroku. If you upload them to the /temp folder they will stay there for a short while or until you re-deploy/update your code I believe.

Related

Ruby on Rails app on Heroku file upload not working

I'm new to Rails and Heroku as well.
Following tutorials I succeed to upload my app to Heroku.
My app is enable us to upload csv file to uploads folder in public.
So I can upload file in heroku deployed app and show table as I want.
But after 30 mins or so those files are removed automatically and showing error that no such file in public/uploads folder.
It is properly working on my local which is using sqlite3.
But heroku want to use psql for production mode so I changed production mode to use psql.
So if I upload csv file then it is stored in 'public/uploads' folder.
I think heroku app is automatically update to github repo even I didn't execute 'git push heroku master' command.
In my local repository files are stored but they are run on development mode.
How can I make upload functionality fully working?
Any help would be appreciate.
Ephemeral filesystem is just the way heroku works, as explained in the article - uploaded files will be removed when the instance is restarted.
What you should do is to upload those files for example to S3 (https://devcenter.heroku.com/articles/s3), you can achieve this with CarrierWave gem with Fog gem but there are other possibilities as explained in this thread here: https://www.reddit.com/r/rails/comments/2k9sq4/heroku_any_files_you_upload_will_not_be_saved/

Deploying a Rails app at heroku with its assets being located at Amazon S3

I'm trying to make a Rails app server its assets at S3. I'm not using any gems because want to do it manually to better understand the process. My questions regarding serving the static assets are:
Which assets should I upload to my S3 bucket: from my_app/assets or from my_app/public/assets?
If the second option, it means I have to precompile them first, right? And then upload the whole folder my_app/public (or my_app/public/assets?) ?
Since right now I'm deploying the website at heroku and the max size of the repo at heroku is 300Mb and the size of my assets is much bigger, how and should I at all make the app somehow "understand" that its assets are located at S3 so that when I say "git push heroku master" it won't upload all them from its my_app/assets or my_app/public/ folder?
You will need to change the config.action_controller.asset_host option on the Rails Configuration. Take a look at http://guides.rubyonrails.org/configuring.html#rails-general-configuration
You will indeed need to precompile the assets and sync them to S3. Remember to use RAILS_ENV=production. A good place to look at and then replicate manually would be in the asset_sync gem.

After each push to Heroku I get 404 errors on images

I am having issues with my Rails App on Heroku. code-dojo.herokuapp.com
After every push to heroku any images I uploaded with Carrierwave Gem return a 404 error message.
Do I need to precompile this folder or point to it ?
Does Heroku replace this folder with a blank one?
Should I create my app with all the images on locathost and then push the database?
Heroku is Read-only Filesystem
The following types of behaviors are not supported:
Caching pages in the public directory
Saving uploaded assets to local disk (e.g. with attachment_fu or paperclip)
Writing full-text indexes with Ferret
Writing to a filesystem database like SQLite or GDBM
Accessing a git repo for an app like git-wiki
You need to use an external storage solution. You can accomplish this for example by using the gem carrierwave-aws instead of the gem carrierwave, with which you can configure an Amazon S3 bucket to store your images on...

Paperclip and Heroku without s3?

I'm trying to upload a file using paperclip in a production environment in Heroku and the log files show:
Errno::EACCES (Permission denied - /app/public/system/photos/1/small/081811-2012-honda-cbr1000rr-leaked-003.jpg):
Will I have to use s3 or similar to handle file uploads, or can I configure path permissions to store the files on Heroku?
Yes Heroku does not allows you to add files dynamically to its server.
Though if you need upload feature on a app on heroku you need to configure s3 or other similar services
Refer this for details
http://devcenter.heroku.com/articles/read-only-filesystem
Yes, you must use S3 or another persistent store like Rackspace cloudfiles, Dropbox etc.
Whilst you can write to the tmp on all the stacks, the Cedar stack does let you write to the file system but it's not shared across dynos or dyno stop/restarts.
See http://devcenter.heroku.com/articles/dyno-isolation#ephemeral_filesystem
Yeah, it is true that Heroku does not allow you to upload files directly onto their servers. However, you can shove your files into your database. I used a gem created by Pat Shaughnessy:
http://patshaughnessy.net/2009/2/19/database-storage-for-paperclip
It worked well.

Using S3/CloudFront with Rails 3 Assets and Less CSS

This one is a mouthful! Basically I'm trying to send all of my Rails 3 assets up to the S3 Cloud and use CloudFront as the CDN to deliver it all. I already learned about configuring Rails to pull from an asset server in production mode. The problem I'm running into is finding a good way to automatically package and send everything to the cloud from a rake command or rails gem. The other problem I have is I don't know if using Less CSS with the More gem is going to screw this up. More generates a CSS file from another directory and places it in public/stylesheets. Any ideas or suggestions are much appreciated! Thanks :)
If you are pushing to Heroku and are using the Rails 3.1 assets you are all set.
In the CloudFront configuration on amazon create your distribution and set the origin to your applications URL.
Then in your production.rb file add:
config.action_controller.asset_host = "xxxxxxxxx.cloudfront.net"
The host is the host of your CloudFront distribution.
Then when you deploy make sure you are on the Cedar stack and that assets are being compiled. This will add a unique MD5 into the filenames. When a request is made to your CDN (handled automatically by the setting in your production.rb file), then the CDN will either serve up it's version of the file or pull it from the origin first. This means you don't have to push files up to the CDN, they are pulled in automatically.
If you have a file that doesn't have a unique name for some reason, then you will need to look at how to invalidate the cache in CloudFront, but other than that it's pretty easy.

Resources