I recently came across a legacy Rails application that had an interesting approach to implementing static pages. This app is basically leveraging on the rails_admin gem to provide a basic form of CMS for building static pages and a controller that parses the friendly slug to render the content. It’s certainly a serviceable solution, however it also comes with it’s own set of complexity such as introducing the friendly_id gem and a category model to separate the content for something that’s unlikely to exceed more than 10 records.
After some thoughts I basically determined that there will ever be just 5 static pages; About Us, Terms of Use, Privacy Policy, Contact Us and FAQ for this project. Enter Thoughtbot’s high_voltage gem. high_voltage was designed to address this very common use-case. If you just need static pages that does not require frequent updates or data driven content, high_voltage fits the bill very well.
To get started, all you need is to bundle the gem in your project.
1 | gem "high_voltage" |
And do the following:
1 2 | $ mkdir app/views/pages $ touch app/views/pages/about-us.html.erb |
Then link the pages like so in your app:
1 | <%= link_to 'About Us', page_path('about-us') %> |
And that’s pretty much all you need to start adding new content.
As a general rule, if a page that you’re building does not require frequent updates or simply does not add any business value or logic to the site, it probably better to build it as a static page with as simple backend like high_voltage. Having static pages as views in your app also gives us the benefit of having highly customized CSS/HTML layout design as opposed to a database backed model which makes high fidelity design and content difficult to insert, eg. managing media assets. However, if your solution does require extensible pages being created, you should probably be looking at CMS solutions such as Radiant CMS than building your own solution anyway.
Be sure to check out their Gem for some idea of how you could still manage custom content within a layout for your static pages.
Thoughtbot – High Voltage
https://github.com/thoughtbot/high_voltage
http://thoughtbot.github.io/high_voltage/
sferik – Rails Admin
https://github.com/sferik/rails_admin
norman – Friendly Id
https://github.com/norman/friendly_id
Radiant CMS
http://radiantcms.org/