How Rubycoders use Contentful cms to publish posts

Cristiano - 2017-03-29

At Rubycoders we are implementing a classic blog system.

We wanted:

  • a blog system with image uploads, related articles, tags, etc.
  • uses the existing design and layouts
  • with clean and readable code

Finally we choose Contentful cms

Contentful is a cloud-hosted content management system that works around a two-pillared philosophy: developer friendliness and isolated content management.

The Contentful team says:

We believe that one CMS shouldn't attempt to please everyone. Whereas most CMS try to be omni-purpose and cover as much use cases as possible, Contentful focuses on several specific applications to truly excel in its realm.

Those specific applications that Contentful is suited for, include:

  • Cross-platform publishing
  • Mobile content management backend
  • Integration with existing applications
  • Rich web experiences

It seems this is the best solution for Rubycoders.

To use Contentful we are following the Rails way, using the gem to connect to the API and creating a classic controller to list and show articles.


Add to your Gemfile and $ bundle :

gem 'contentful'

Then configure the routes for the articles_controller

resources :articles, only: [:index, :show]

Now we need to connect to the Contentful API to fetch the articles:

class ArticlesController < ApplicationController

  def index
    @entries = client.entries content_type: Rails.application.secrets.contentful_content_type

  def show
    @entry = client.entries(
      "fields.slug" => params[:id],
      content_type: Rails.application.secrets.contentful_content_type).first.fields

  def client
      access_token: Rails.application.secrets.contentful_access_token,
      space: Rails.application.secrets.contentful_space

To fetch articles we want to use the slug instead the id, becouse if we use the id we have an url like this:

but we prefer to have:

To do this in the index view we have:

<%= link_to @entry.fields[:title], article_path(entry.fields[:slug]) %>

So in the show action we need to fetch the article by slug:

@entry = client.entries(
  "fields.slug" => params[:id],
  content_type: Rails.application.secrets.contentful_content_type).first.fields

where params[:id] is the slug.

Using this system we have a database-less blog system for free without install extras blog engine or adding heavy gems.