Skip to main content

Deploying a Rails app on Heroku (paperclip, gems, yml)

·470 words·3 mins
Author
Yang Chung

I just moved my PlaygroundsRUs site from AWS to Heroku, and I couldn’t be happier. My first full month bill from Amazon was about $75, and $74 of that (99% of the total cost) was for running an instance. Harlan told me about Heroku after he deployed his ForkThis demo on there.

Most of the transition was smooth, but there were a few hiccups on the way. One of them still hasn’t been resolved (one of the plugins is having conflict with PostgresSQL, which is used by Heroku). I will enumerate what I had to go through so that it might be easier for you.

1. Secret YAML files

I have a public GitHub account for deploying on AWS using ec2onrails. Since it’s public and anyone can see it, I had to omit sensitive .yml files in config directory that contained passwords and keys. It’s easily done by specifying those files in .gitignore and listing them in :nonvc_configs in config/deploy.rb used by ec2onrails.

I found out that for Heroku, the same can be achieved by creating another branch, including those files, merging with master, and pushing it to Heroku. So, the following lines should do the trick.

git checkout production
[remove those yml files from .gitignore]
git merge master
git push heroku production:master

2. gems

It pays to read through documentations. I thought it might be easier to install/unpack gems in vendor directory, since I wasn’t sure how gems would be installed for my app on Heroku. But, they made it very easy. Just follow direction here and list those gems required by your app in .gems file. Can’t get any easier than that.

3. paperclip

Last is paperclip. I use it to upload pictures for each playground object. Since you can’t write direct on Heroku’s file system, it makes sense to use Amazon’s s3. Heroku document actually says that Heroku runs on the Amazon platform and thus s3 transfer is free.

Just like ec2onrails, you can use s3.yml to specify access_key and access_secret. You could also follow direction on Heroku here, but I found out that Heroku’s example is missing one critical parameter, :path. Without :path parameter, you will get an error. There is an excellent example of using paperclip on Heroku by Pedro. It’s definitely worth checking out.

So, my model that accepts attachments have the following lines.

has_attached_file :data,
 :styles => {
 :thumb => "50x50#",
 :large => "600x400#"
 },
 :storage => :s3,
 :s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
 :path => ":attachment/:id/:style.:extension"

While my s3.yml looks like the following.

staging:
 access_key_id: [Your Key]
 secret_access_key: [Your Secret]
 bucket: [Your bucket name]

production:
 access_key_id: [Your Key]
 secret_access_key: [Your Secret]
 bucket: [Your bucket name]

Oh, and there is Firefox plugin - S3Fox - for managing Amazon s3. It’s SO good. You gotta check out. I love the whole rails community and Firefox. They rock!