Software Developer, Cyclist, & Traveler.

The Super Simple Way to Add a Jekyll Based Blog in Your Rails Application

In my original post I talked about setting up Jekyll inside your existing Rails application via gems and modifying your config.ru file. We can simplify the process even more than that though.

Jekyll is just a static site generator so all we need to do is be able to publish it within our Rails application’s public directory and our web server will take care of it from there. It also doesn’t need to be coupled to our Rails application, it can just sit in our Rails application, similar to static uploads.

What we’ll be doing is just adding a shared directory to our deploy recipe for where the blog will go. From there we can just build and rsync the Jekyll site to our blog directory. Easy peasy.

I’m going to start from scratch but if you already have a Rails application feel free to skip this step.

Now that we have our Rails application running in ~/src/little-feedback/little-feedback we can create our blog in a sibling directory.

$ cd ~/src/little-feedback
$ gem install jekyll
$ jekyll new blog

Once Jekyll finishes creating your blog, modify your _config.yml file’s baseurl option to point to /blog. This is important so that assets load and everything links properly.

baseurl: "/blog" # the subpath of your site, e.g. /blog

Now we have our blog in ~/src/little-feedback/blog and our Rails application in ~/src/little-feedback/little-feedback. To demo the setup we’ll add a deploy script that just builds Jekyll and rsyncs it to our local Rails application’s public directory. This flow will be identical to how it’ll sit on the server it’ll just be an rsync to your server(s) to the appropriate directory instead of a local file path.

With blog/ added as a linked directory you can deploy whenever and the blog directory will persist with your Jekyll blog. Now you’ll just need to update your bin/deploy script to rsync to the proper directory on your server, something like the following: