Using Contentful with a Ruby on Rails app

This guide is a follow up walkthrough to our Rails getting started tutorial. It will provide you some guidance onto how to use Contentful within a new Rails Application created from scratch.

Create a Rails Application

The first step to get you started is the creation of the Rails application itself.

You can do so by running the following command:

$$ rails init `<YOUR_PROJECT_NAME>`

Add Contentful to your project dependencies

On your project’s Gemfile:

1gem 'contentful'

Decide how to include Contentful

There are many ways to include Contentful on your project, here we will introduce two of the ones that we consider the simplest. These will get you started to consume content as fast as possible.

Contentful as a View Helper

If your project uses a single Contentful Space and reuses content across multiple pages that are not necessarily tied to your models, you may want to use this approach. It will let you use your Contentful client across your views.

On app/helpers/application_helper.rb:

1def contentful
2 @client ||= Contentful::Client.new(
3 access_token: ENV['CONTENTFUL_ACCESS_TOKEN'],
4 space: ENV['CONTENTFUL_SPACE_ID'],
5 dynamic_entries: :auto,
6 raise_errors: true
7 )
8end

On your views then you can use it like follows:

1<% products = contentful.entries(content_type: ENV['CONTENTFUL_PRODUCT_CT_ID'], include: 2) %>
2<% products.each do |product| %>
3 <div class="contentful_product">
4 <h1><%= product.title %></h1>
5 <img src="<%= product.image.first.image_url %>" />
6 <p><%= product.description %></p>
7 <a href="<%= product.website %>">Buy Now!</a>
8 </div>
9<% end %>

Contentful as a Concern

If your project uses a single Contentful Space but each model instance is related to a specific Entry in Contentful, the following approach will allow you to accomplish this.

  • Create app/models/concerns/contentful_renderable.rb

  • On the newly created concern, add the following code:

1module ContentfulRenderable
2 extend ActiveSupport::Concern
3
4 def self.included(base)
5 base.extend(ClassMethods)
6 end
7
8 # Overridable
9 # Override this method to change the parameters set for your Contentful query on each specific model
10 # For more information on queries you can look into: https://www.contentful.com/references/content-delivery-api/overview
11 def render
12 self.class.client.entries(content_type: self.class.content_type_id, include: 2, "sys.id" => contentful_id).first
13 end
14
15 module ClassMethods
16 def client
17 @client ||= Contentful::Client.new(
18 access_token: access_token,
19 space: space_id,
20 dynamic_entries: :auto,
21 raise_errors: true
22 )
23 end
24
25 # Overridable
26 # Override this method to change the parameters set for your Contentful query on each specific model
27 # For more information on queries you can look into: https://www.contentful.com/references/content-delivery-api/overview
28 def render_all
29 client.entries(content_type: content_type_id, include: 2)
30 end
31 end
32end
  • Add the fields to your Model:
$$ rails g migration add_contentful_fields_to_`<MODEL_NAME>` contentful_id:string
  • Include Concern on your Model:
1class MyModel < ActiveRecord::Base
2 include ContentfulRenderable
3
4 self.space_id = YOUR_SPACE_ID
5 self.access_token = YOUR_ACCESS_TOKEN
6 self.content_type_id = YOUR_CONTENT_TYPE_ID
7
8 # ... your regular model stuff ...
9end
  • Now you can use it on your views as follows:

Assuming our model is a Product

  • For fetching a single entry:
1<% @product.render.tap do |product| %>
2 <div class="contentful_product">
3 <h1><%= product.title %></h1>
4 <img src="<%= product.image.first.image_url %>" />
5 <p><%= product.description %></p>
6 <a href="<%= product.website %>">Buy Now!</a>
7 </div>
8<% end %>
  • For fetching the collection:
1<% Product.render_all.each do |product| %>
2 <div class="contentful_product">
3 <h1><%= product.title %></h1>
4 <img src="<%= product.image.first.image_url %>" />
5 <p><%= product.description %></p>
6 <a href="<%= product.website %>">Buy Now!</a>
7 </div>
8<% end %>

Using the Concern approach makes each of the instances of your model configurable. If you want to pull out the configuration to a separate object, you are free to add a belongs_to relationship. That way you can separate your Contentful configuration from your models, this will require some changes on the concern.

Query data from Contentful

As you might have seen in both previous examples, we’re sending parameters to the Entries API endpoint. To learn more about those parameters, you can look into our search parameter documentation.

Conclusion

With this basic guide, you should be able to start using Contentful within your Rails applications.

For learning how to create complete Rails applications we recommend the official Rails Getting Started Tutorial.

You can read about the Contentful CDA library in more detail on our contentful.rb GitHub or our Getting Started with CDA client library tutorial and take a look at our Contentful Rails Example Application.

Also check our Rails integration libraries contentful_model and contentful_rails, which provide a more Rails-like approach to create Contentful based applications.

Do you like building static sites? Maybe you want to check how to build static sites using Contentful with Middleman or Jekyll.

In case you you want to set up your new spaces via the command line we also provide Contentful Bootstrap.