The Golden Middle Path - a blog by Amit K Mathur

Dead Simple CMS

We all need to create the static pages with an application — pages like FAQ, About-us, Terms and conditions etc. Of course, it would be extra nice if the site admin can edit those pages without having to deal with the source control system and the like: you know, checking files out of git or svn, making changes, checking it back in and figuring out how to deploy it all on the server. Ah, just try explaining that to your site admin before her eyes rollover, she falls backward with her chair and breaks a rib or two.

Enter CMS (Content Management System). There are a lot of them available in the Rails world. For a nice list see here. Perhaps you just want to pick a plugin from there and run. However, what if you want something really simple, something that just supports the 6 static pages you need and does not add much unneeded clutter to your code base. Turns out you can roll your own really easily. Follow along.

Firstly, create a model StaticPage – where each instance of the model is one static page you need. For example, you can have a model like the following:

# from root of your Rails project
$ ruby script/generate model StaticPage title:string content:text slug:string
$ rake db:migrate

You will need to make the slug unique. The model should look like:

# FILE: app/models/static_page.rb
class StaticPage < ActiveRecord::Base
  validates_presence_of :slug
  validates_uniqueness_of :slug
end

That’s all for the model. Now, we need to setup the controller and the views. So, let’s say any URL like /info/contact, /info/faq or /info/about points to a static page in the application. So, put the following near the bottom of the routes file:

# static pages
map.info_page '/info/:page_name', :controller => 'home', :action => 'info_page'

The controller and action could of course be any generic controller that you already have in the application. The static action is quite simple – it just fetches the content of the static page from the model:

# FILE: app/controllers/home_controller.rb
def static
    @page = StaticPage.find_by_slug(params[:page_name])
    if @page.blank?
      render :template => 'public/404.html', :layout => false, :status => 404
    end
end

And the view displays the content:

# FILE: app/views/home/static.html.erb
<h1><%= @page.title %></h1>
<%= simple_format(@page.content) %>

Here, I have used Rail’s built-in simple_format helper. You can of course run the content through a more fancy markdown filter like Textile.

We also need a way for the site admin to easily edit the content. That is best done by making static_pages a restful resource. Starting from routes:

# FILE: config/routes.rb
map.resources :static_pages
# FILE: app/controllers/static_pages_controller.rb
standard restful controller with index, new, create, edit and update actions and corresponding views for index, new and edit. Just be careful to provide access to only admin users.

And we are done with our simple CMS. Any time admin can edit an existing page or even create a new one. Isn’t that simple.

Share:

Post a comment


(Formatting help)