Contentful on Rails: two fresh gems

July 21, 2016


We've reworked two gems which simplify building Contentful-powered Rails apps — contentful_model and contentful_rails — and are eager to tell you all about them.

First off, some history

Two years ago our friends from Error Studio created contentful_model and contentful_rails — two gems to make your Rails + Contentful development as easy as it can get, using what you already know from Rails to power your Contentful projects. Since then many many features have been added to the project, allowing Rails developers to fetch content directly from Contentful in the Rails way.

Revealing the hidden gems

Sadly, Error discontinued development in late 2015. We still wanted to continue providing support for the active users, so early this year we got the ownership of these gems and started building some long-requested features.

The future is green (just as our tests)

Today we're glad to relaunch (in a beta mode for now) the contentful_model gem, which now includes content migrations and entry editing via the CMA. The gem is extensively covered with tests, and the documentation has also been refined.

contentful_model features

Simple ActiveRecord-like API for defining content models

class MyEntry < ContentfulModel::Base
  self.content_type_id = 'my_entry'

  has_many :other_entry
  has_one :some_cool_entry
  # .. and more ..

# Get all entries

# Get a single entry

# Search by field
MyEntry.find_by(foo: 'some value').load

# Full Text Search [by field] 'some value inside my text').load

# Full Text Search [all fields]'some text inside any field').load

Content editing/publishing with the CMA integration

my_entry = MyEntry.find('some_id') = 'bar' # Saves edits as a draft

my_entry.publish # Publishes entry

# You can also chain the commands # Will save and then publish

Content model migrations compatible with ActiveRecord::Migration

class CreateFooContentType < ActiveRecord::Migration
  include ContentfulModel::Migrations::Migration

  def up
    create_content_type('foo') do |ct|
      ct.field('bar', :symbol)
      ct.field('baz', :date)

    add_content_type_field 'foo', 'foobar', :integer

  def down
    remove_content_type_field 'foo', 'foobar'

What's coming next

As most things in life, these gems will always be getting better. We plan to extensively cover contentful_rails with tests and add some more helpers to help you build things faster.

Another aspect of our roadmap is the feedback-driven improvements — we're attentively reading your tickets and listening to everything you have to say to make these gems ready for a final release.

Your contributions are welcome

You can use these gems inside your apps and provide us feedback for contentful_model and for contentful_rails — just open an issue on GitHub or submit a pull request. We're looking forward to what you have to say!

There's more for the Rails crowd

If you're looking for more ways to integrate Contentful with Rails apps, there's a nice tutorial which will help you get started.

About the author
Don't miss the latest
Get updates in your inbox
A monthly newsletter to help you build better digital experiences with Contentful.
add-circle remove style-two-pin-marker subtract-circle