Getting Started with Contentful
This guide will walk you through your first steps using Contentful within a Rails application. It will provide a step-by-step guide on how to get your first entries and start using the content you create on Contentful.
- A Heroku account
- Ruby 2.3.1 (via RVM or rbenv)
- Postgres installed
- Git installed
- Basic Command Line Interface, Ruby and Rails knowledge
Start from a demo application
First we'll start with a demo project, which is a simple Rails application using Contentful's 'Product Catalogue' template, so that you can see it running for yourself.
Run the following commands to get started.
- Clone the example repository:
git clone https://github.com/contentful/contentful_rails_tutorial.git
- Navigate into the repository's directory:
- Install dependencies:
- Install default Contentful configuration:
bundle exec rake db:create db:migrate db:seed
- Run the server:
bundle exec rails s
Everything is now set up. You can view your new data by opening _http://localhost:3000/contentful_users/1_ in your browser.
It should look something like this:
Using your own content
You can create your own custom data by following these steps:
- Create an account with Contentful or Log In.
- Create a new Space with the 'Product Catalogue' template, name it whatever you like.
Note: Creation of a space may result in additional charges if the free spaces available in your plan are exhausted.
- In the Rails application:
- Click the Edit Demo Configuration button on the top navigation bar.
- Click the New Contentful Configuration button and fill in the form as follows:
- Configuration name: Choose a name for your configuration.
- Space: Add the value from the Contentful web app > APIs > Content delivery/preview keys > Website key > Space ID.
- Access token: Add the Production Delivery API Access Token from the Contentful web app > APIs > Content delivery/preview keys > Website key > Production.
- Content type: Add the Product's Content Type ID from the Contentful web app > APIs > Content model explorer > Product > Identifier.
- Your space will be now displayed in your application
Next in the Contentful web app > Content:
- Open the product called 'Playsam Streamliner Classic Car, Espresso'.
- Change the value of the Product name field to a new value,
- Click the Publish changes button.
- Wait a few seconds for the changes to propagate to the CDN.
- Reload your Rails application and you will see the new product name.
You can continue to edit your content inside the Contentful web app and see the content change inside your Rails application.
Deploy the demo to Heroku
To view the demo application live in your own production environment, follow these steps:
- Install the Heroku toolbelt if you don't already have it.
- Login to Heroku if you're not already:
- Create a new instance:
- Create a Postgres database:
heroku addons:create heroku-postgresql:hobby-dev
- Update the config/database.yml file:
VALUE_OBTAINED_FROM_YOUR_HEROKU_PG_CONFIG with the database name in your Heroku Database's Dashboard. You can find the name at Heroku Database Dashboard > Your Postgres instance > Database.
- Commit your change:
git add . git commit -m "Update Database Configuration"
- Deploy to Heroku:
git push heroku master
- Install your default Contentful configuration:
heroku run env bundle exec rake db:migrate db:seed
- Open the application in your browser:
After this guide, you should be able to start using Contentful with your Rails applications, but every project has different needs and we want to provide you with the best solutions we can.
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, we also suggest taking a look at our Contentful Rails Example Application.
If you want to create your own Rails project we recommend following this tutorial, or our Rails integration libraries
contentful_rails, which provide a more Rails-like approach to creating Contentful based applications.
If you want to set up new spaces via the command line we also provide Contentful Bootstrap.
Not what you’re looking for? Try our FAQ.