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:

gem '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.

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.