Hello! In this tutorial, we’ll show you how to add feature flags to your Ruby on Rails app, using Unleash and the official Unleash Ruby SDK. With Unleash, an open-source feature flag service, you can use our tooling to add feature flags to your application and release new features faster.
In a classic tutorial fashion, we’ll add feature flags to a blog app made with Ruby on Rails. We’ll use feature flags to decide how many blog posts to show on the index page.
For this tutorial, you’ll need the following:

The Unleash Server is a Feature Flag Control Service, which manages your feature flags and lets you retrieve flag data. Unleash has a UI for creating and managing projects and feature flags. There are also API commands available to perform the same actions straight from your CLI or server-side app.
Rails is a back-end framework, so there are special considerations to plan around when implementing feature flags.
Most importantly, you must:
For a complete list of architectural guidelines, including caching strategies, see our best practices for building and scaling feature flag systems.
In this section, we’ll install Unleash, run the instance locally, log in, and create a feature flag. If you prefer, you can use other tools instead of Unleash, but you’ll need to update the code accordingly. The basic steps will probably be the same.
Use Git to clone the Unleash repository and Docker to build and run it. Open a terminal window and run the following commands:
You will now have Unleash installed onto your machine and running in the background. You can access this instance in your web browser at http://localhost:4242.
Log in to the platform using these credentials:
Click the ‘New feature flag’ button to create a new feature flag.

Call it top-3 and enable it in the development environment.

Everything’s now set up on the Unleash side. Let’s set up the Rails application.
Let’s clone a basic blog repository and get it up and running. We don’t want to waste time setting up a Rails codebase from scratch.
This repository uses SQLite, so no additional dependencies are required.
Run the migration
Seed the DB
Go to http://localhost:3000 and check that you see the following:

Right now all the blog posts are displayed on the index page. We want to use a feature flag to change that and restrict it to the three most recent posts.
Let’s create a static boolean flag, for now.
Modify the index method in app/controllers/posts_controller.rb to look like this:
Reload your browser and you should see only the 3 most recent posts.
Now, let’s connect our project to Unleash so that you can toggle the feature flag at runtime. If you wanted to, you could also do a gradual rollout, and use it for A/B testing or more advanced functionality.
You’ll need 2 things:
http://localhost:4242/api/ for your local version. You’ll want to replace it with your remote instance.First, install the unleash gem.
Then, create config/initializers/unleash.rb, and add the following:
You can check our API tokens and client keys documentation for more information.
Then, in app/controllers/application_controller.rb, add the following method:
Now, let’s add the Unleash client to our Rails controller, grab the feature flag from our local instance, and update our conditional statement. Don’t forget to also update the config with your API key.
Reload your browser and check that you see three blog posts displayed. Turn off the flag in your Unleash instance and reload the page. You should see all the blog posts again.
See additional use cases in our Backend SDK with Ruby documentation.
Note: An update to a feature flag may take 30 seconds to propagate.
All done! Now you know how to add feature flags with Unleash in Rails. You’ve learned how to:
Thank you