Contentful plus Ruby

Introduction

KB4

If you love working with Ruby, but you haven’t found a reliable content management system (CMS), and the lack of choices underwhelms you, we know how you feel. We know because we’ve been there too. That’s why we’re proposing a new solution: an API-first, technology agnostic content platform that you can use in your Ruby applications.In this article, we’ll highlight the benefits of using Contentful and our content platform for your next Ruby project.

What is Contentful?

Before we go any further, it’s important to clarify what you’ll be getting with Contentful. Through our content platform, your developers will be able to fetch content with API calls, while offering editors a familiar-looking web app for creating and managing content.So whether you’re developing for wearables, smartphones, or web apps, your content is just an API call away. And because it runs as a hosted solution in the cloud, it requires far less maintenance than a traditional Ruby content management system—leaving you more time to focus on building great applications.

Getting started with our Ruby SDKs

We provide SDKs for popular programming languages like JavaScript, PHP, and, of course, Ruby. These SDKs are designed to make your life as a developer easier when using Contentful and give you almost instant access to our APIs and their features.

Content Delivery API (CDA)

Contentful’s Content Delivery API (CDA) is a read-only API for delivering content from Contentful to apps, websites, and other media—arriving as JSON data and media files.To get started with our Ruby CDA SDK, you have two options.Install the 'contentful' gem:

Or include it in your 'Gemfile':

Once this is in your 'Gemfile', you can run 'bundle install' to install the gem and all of its dependencies.

Then, you can work with the following code snippet to fetch content from Contentful:

And there’s so much more where that came from. If you want to dive deeper, we’ve explained this entire process in our getting started tutorial.

Content Management API (CMA)

We also have a Ruby SDK for Contentful’s Content Management API (CMA). This is a read-write API for managing content. You can use the CMA for several use cases, including automatic imports from traditional CMSes like WordPress or Drupal and building custom editing experiences.

Again, you have two choices: Either install our 'contentful-management' gem as outlined above or add it inside your 'Gemfile' and run 'bundle install'. If you choose to include it in your 'Gemfile', the only difference is what exactly you add:

Once that is complete, you can then work with the following code snippet to manage content from Contentful:

For those interested in learning more, we also have a guide on getting started with the Contentful CMA and Ruby.

Integrations with Rails and Sinatra frameworks

You’re probably using a framework for your Ruby applications—and for a good reason. Frameworks enable you to scale quickly, code maintenance is more manageable than with plain Ruby, and they allow you to invest your time in the actual task instead of the supporting technology.Don’t worry, we’ve got you covered. Contentful has integrations for two of the most popular Ruby frameworks: Rails and Sinatra.

Rails

Ruby on Rails, or Rails for short, is arguably the most popular Ruby framework. It’s designed to make programming server-side web applications with Ruby hassle-free by enabling you to write less code while accomplishing more.To facilitate this collaboration, we’ve reworked two gems that simplify building Contentful-powered Rails apps: 'contentful_rails' and 'contentful_model'. These gems were created to empower Ruby developers to integrate Contentful into their Rails applications easily. You can read more about how to use 'contentful_rails' and 'contentful_model' in your applications on our blog.The best way to get going with Contentful and Rails is to work through our Using Contentful with a Ruby on Rails app tutorial. While it doesn’t use the gems mentioned previously, it is the most comprehensive guide out there.

Sinatra

Also commonly found in the Ruby community, Sinatra is a DSL that works under the philosophy of “creating web applications in Ruby with minimal effort.”Here at Contentful, we love using Sinatra because of its simplicity and flexibility. In fact, we’ve built our Example App with it. You can check it out on GitHub. In this app, we’ve integrated Contentful by using the Service pattern. This was because of needing to swap between the Delivery and Preview APIs dynamically, and we had a few very specialized queries as well. You can take a look at how we wrote it in our wrapper file—plus, this line from our 'courses.rb' file is an example of how we use it.If you’re looking for a quick way to get started, you can follow our tutorial on creating your own Contentful and Sinatra application.

Generating static sites with Middleman or Jekyll

There’s no denying that static sites have made a comeback — largely because not every web project needs a complex technical stack behind it. Sometimes a handful of static pages are enough. That’s why we’ve built Contentful integrations for Middleman and Jekyll, two well-liked and Ruby-based static site generators. Each of these integrations are described in detail in our blog post, “Building static sites with Contentful and Ruby".We have multiple tutorials on Middleman and Jekyll, showing features like the automated rebuild cycles and other example repos. On top of that, one of our Ruby evangelists David Litvak also has a pretty extensive demo of what you can do with Contentful and Middleman on his GitHub. In this project, the most important file to pay attention to is the 'config.rb' because, as David says, this is “where all the magic is actually happening.”

What’s next

We encourage you to check out the Contentful for Ruby for tutorials, example apps, and more. Or if you’re ready to jump right in, create a free account and start interacting with our content infrastructure today.