This is a blog of AmberBit - a Ruby on Rails web development company. Hire us for your project!

Building small sites with LocomotiveCMS and deploying to Heroku and GridFS

So you have a small site to build and host cheaply. Let’s user Rails. Let’s deploy to Heroku. Let’s build around Locomotive CMS. Let’s see how.

LocomotiveCMS is lightweight CMS solution built on Rails and MongoDB. To be fair, I have love-me-hate-me relationship with MongoDB, but it definitely suits great for storing records with dynamic/not specified schema. This is perfect for CMS systems, where you want to define types of records as you go.

LocomotiveCMS documentation says you have to use Amazon S3. But you have limited budget or simply don’t want to include external dependency. As Locomotive CMS is already using Carrierwave and Mongoid, it should be possible to store uploaded files in MongoDB’s GridFS, right?

First, follow the excellent guide at Locomotive CMS site to get your site running on Heroku: http://doc.locomotivecms.com/installation/heroku

You can choose to use MongoLab or MongoHQ, I opted in for MongoLab, so I had to install their addon and replace MONGOHQ_URL with MONGOLAB_URL in documentation above, but that was all.

To actualy store files in GridFS instead of S3, you’ll have to modify you config/initializers/carrierwave.rb to include:

config.storage = :grid_fs
config.grid_fs_connection = Mongoid.database
config.grid_fs_access_url = "/uploads"

That will make all assets and template files to be uploaded in GridFS. However, there is additional step to serve those. You need rack middleware to send out uploaded files, when user requests them: Add to your Gemfile:

gem 'rack-gridfs', :require => 'rack/gridfs'

run “bundle install”, and modify your config/application.rb with the following:

require 'uri'
.....
uri = URI.parse(ENV['MONGOLAB_URI'])
config.middleware.insert_after Rack::Runtime, Rack::GridFS,
 :prefix => 'uploads', :lookup => :path,
 :database => uri.path[1..-1], :hostname => uri.hostname,
 :port => uri.port, :username => uri.user, :password => uri.password

Push to Heroku and voilà!

by Hubert Łępicki, twitter: @hubertlepicki

Do you need skilled professionals to help you build Rails applications? Hire us for your project!
comments powered by Disqus

Want to get in touch? Drop us a line!