This is a tutorial post about a gem I have just released: manageable_content. It is a content management framework for Rails 3.1. It provides a basic mapping between controllers to manageable contents. More detailed info can be found on the README file.
In this post, I’ll show how to create a new Rails project using the engine. The full example is available in Github. So, let the fun begin!
The first step is to create a new Rails app. Right now the engine requires Rails 3.1, so be sure to have it installed:
So, the ideia of the website is to allow each page to have its own title in a manageable way. We will also have two controllers, Home and Contact. Home controller will have a body content, and Contact will have a body and a side content. Also, we need a content for the footer that will be shared across all pages.
Let’s generate our controllers first:
In this commit I have added sample contents so that you can see which contents we want to be manageable.
Ok, now we need to add manageable_content to our Gemfile. This line needs to be added:
Then, run the ‘bundle’ command to install it. After that, we need to import and run the engine migrations:
The next step is to make the engine Dsl available for our controllers. We need to add this to our application_controller:
We will start with our title content. Let’s make the title content manageable for all controllers in our website by adding this to our application_controller:
This command will generate our contents in the database based on our controllers configurations. We should than use the engine helper to print the manageable content in our views. You can see how I have done that in this commit. Now, we need a place in which we can edit our manageable contents. In this commit and in this one I created a basic administration page to manage the pages. An important thing to remeber is that the admin controller should inherit from a controller different that the application_controller, otherwise your admin controller will have manageable contents too!
Now, by accessing the admin page and saving some title contents, if we access our website pages their titles should be using the manageable contents. In our pages admin, you can notice that there was a page for our home controller, contact controller and for the application controller. To use the application controller title in our views, we have to use the manageable_layout_content_for helper. Check this commit.
We can also have content that will be shared by all pages. In our website, the ‘footer’ content has to be like that. On our application_controller, we add this:
Than, after generating our contents with ‘bundle exec rake manageable_content:generate’, the footer content should be list under the ‘application’ page on our admin. To use it in our views, just add this:
The commit for this step is this one.
To finish this example, we will add contents that will be specific to our home and contact controller. Home controller will have a body content, and contact will have a body and a side content. Add this to the controllers:
After generating the contents, we can manage them thought the admin interface, and use them in our views with the helper, as you can see here.
So basically, that’s it!
The ideia was to create a simple to use and generic content manager for Rails. The source code is available in [GIthub](https://github.com/fabiokr/manageable_content) and it is open for collaboration.