I have built an application recently but also wanted to integrated some VERY simple content management system (maybe an inline edit) for static pages on the marketing site. I saw a bunch of examples on how to do this but nothing really seemed to work for how I wanted to be able to tackle this problem. For starters, I wanted to be able to use other Rails Content and Models if I needed to display statistics or other Model info. I wanted to use the CSS and JS Packaging as well as layouts in Rails that I had already built. I also wanted to make the pages into subfolders for easier updating and organizing in the future, which plays into seo. Here is what I came up with to solve this issue. I hope it helps someone else as well…ENJOY :)
1) Rails Controller to handle the static page
This controller seems to be the general catch all for any file in any folder. The reason I set the path Param is so that we can use that to set the page name on the html body as a class
class PagesController < ApplicationController respond_to :html #Uncomment if you want cache caches_page :index, :show def index # Home Page Only = / end def show @path = params[:path] #Path Param (example /about/faq) template = File.join('public/pages', @path) template_with_index = template + "/index" render :template => template #Normal File rescue render :template => template_with_index #if you had an index.html.erb file under a subfolder rescue redirect_to "/404.html" end end
2) Route
The route is a catch-all style route to be put after all other routes are listed
resources :pages, :only => [:index, :show] match '*path' => 'pages#show' root :to => 'pages#index'
3) The Views and Layout
The updates to the views and layouts were to provide an easier way to write css
<body class="<%= page_name %>">
3) The Helpers
page_name is a helper method which will generate a variable based on the path params
module ApplicationHelper def page_name @path.parameterize('_') unless @path.blank? end end
I felt this simple implement allows me to utilize all the beauty of Rails but doesn’t limit me to having to use public/*.html files, which isn’t easy to use when you already have layouts and styles. I thought about using some different gems to handle this but most seemed a bit overkill for what I wanted to do and this method was easier for me, in my opinion. I hope this helps someone else that is considering doing something like this as well.



