Contentful plus Ruby
Updated: October 26, 2020
Content infrastructure for your next Ruby project
Introduction
![Contentful + Ruby feature Image](http://images.contentful.com/fo9twyrwpveg/6gmOu01lKwkAikCguMMy68/6ace8cfa23813d2fa3df7736a276e82c/CF_Ruby.png)
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
textgem install contentful
Or include it in your
Gemfile
textgem ‘contentful’, ‘~> 2.3.0’
Once this is in your
Gemfile
bundle install
Then, you can work with the following code snippet to fetch content from Contentful:
textrequire ‘contentful’ client = Contentful::Client.new( space: SPACE_ID, access_token: DELIVERY_TOKEN, dynamic_entries: :auto, raise_errors: true ) entries = client.entries
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
Gemfile
bundle install
Gemfile
textgem ‘contentful-management’, ‘~> 1.0’
Once that is complete, you can then work with the following code snippet to manage content from Contentful:
textrequire 'contentful/management' client = Contentful::Management::Client.new(MANAGEMENT_TOKEN) space = client.spaces.find(SPACE_ID) entries = space.entries.all
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
contentful_model
contentful_rails
contentful_model
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
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
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.