Static Pages with High Voltage

In the last couple of Rails apps that I’ve been working on, I’ve needed at least a couple of static pages; nothing fancy, just a landing page and an about page. This need lead to a common anti-pattern I was running into that involved creating a controller called HomeController that contained empty methods that were used for routing to the associated views.

# app/controllers/home_controller.rb
class HomeController < ApplicationController
  def home

  def about

With code, I know less is more, but this is a little much. Why even have this controller if it will be essentially empty?

As with most problems I run into, there is a gem for that.

High Voltage is a gem for easily serving static pages, and it does that one thing very well. After adding High Voltage to your Gemfile and running bundle all you need to do is create a app/views/pages directory and put your erb files there. Then your pages will be served with a /pages/:id route where the id is the filename. There are some more options and overrides you can make, but the one configuration that I do make is using the High Voltage router for route drawing.

# config/initializers/high_voltage.rb
HighVoltage.configure do |config|
  config.route_drawer = HighVoltage::RouteDrawers::Root

This allows an about page that would normally route through pages/about' to be available though/about`. I make sure to enable this because I am a neat freak and don’t like having to use the “pages” prefix for routing.

I wish I had known about this gem before, it would have saved me some pointlessly empty controllers.