How to use Flipper for feature flags in Rails

Feature flags are used all across the tech industry to test code at scale; this article explores how to integrate them into your Ruby on Rails application using the Flipper gem.

Feature flags, sometimes called feature toggles or switches, are used to enable or disable features or functionality in a software application without requiring a new production deployment. Feature flags placed around sections of code empower developers to easily and quickly deploy new features to a production environment while reducing the risk of impacting users or the overall system.

Developers can deploy code changes to a small group of users or a specific geographic region, and then gradually roll out the changes to a larger audience once they have been validated at scale. Conversely, feature flags can also be used to quickly roll back changes if any issues or bugs are discovered.

Flipper helps Ruby developers use feature flags in their codebase with powerful add-ons, most notably a dashboard to toggle features while code is in production. This article will explain when it's appropriate to use flipper instead of manually writing feature flags, as well as how to securely implement Flipper into an existing application.

Writing a feature flag manually

It's not too challenging to wrap a section of code around an if statement to manually create a feature flag. If you want a section of code to only run 50% of the time, you can simply use a conditional and a random number.

The Ruby built-in rand function makes this straightforward and readable. If you want to redirect roughly half of the requests, you would do something like the following:

if rand < 0.50
  # Handle request normally

def redirect_to_beta_interface
  # Redirect code would go here

The biggest problem with this strategy is that any changes to the frequency at which the feature is enabled must be done through a production deployment of a code change. For some things, this may be fine, but teams often want a more on-demand way to enable or disable a feature.

Installing Flipper

The Flipper Ruby Gem empowers developers with more control over features in real time. Installing it starts with adding Flipper, Flipper-ui, and the storage adapter to your Gemfile:

gem 'flipper'
gem 'flipper-ui'
gem 'flipper-active_record'

Next, run the following:

bundle install

Next, generate Flipper's required migrations:

bin/rails g flipper:active_record

Finally, run the generated migrations:

rails db:migrate

Checking a feature flag in Ruby

Now that you've installed Flipper, you can use the gem to check whether a section of code should be executed:

if Flipper.enabled?(:beta_interface)
  # Handle request normally

def redirect_to_beta_interface
  # Redirect code would go here

Notice how closely this code matches our simple hand-spun feature flag. This makes it easy to transition existing feature flags to Flipper, where the greatest benefit comes in when toggling features for subsets using the UI.

You could also abstract out feature flags checks into a before_action, which is Rails conventions would likely smile upon. Our above example might look something like this if you're adding a before_action to a controller:

before_action :ensure_beta_interface_enabled

# Other controller methods located somewhere in the file

def ensure_beta_interface_enabled
  render :unavailable unless Flipper.enabled?(:beta_interface)

Setting a feature flag in Ruby

Although it's uncommon, you can set feature flags in code if you want to programmatically enable or disable a feature. This might be useful if you're disabling a feature due to an error that the application would be able to catch.

To enable a particular feature, such as our beta_interface example, for all requests and users, you would call the following:

Flipper.enable :beta_interface

You can also enable a feature for a certain user, certain groups of users, or some percentage of requests programmatically. The Flipper docs go into further detail.

Managing features with the Flipper UI

Before accessing the UI, you must first mount the UI to your Rails routes by adding the following to config/routes.rb, along with your other routes:

mount => '/flipper'

Now you can go to your application's flipper UI by appending /flipper to your root URL. The UI allows you to enable features for all users, certain users, or certain percentages of requests. Currently, the Flipper UI does not require authentication, which could cause security problems.

The Flipper UI documentation outlines a few ways that the UI can be secured. The documentation suggests using a route constraint and integrating with either rack or whatever authentication system already exists in the application.


We've seen how using feature flags in Rails applications can provide you with a robust tool for releasing new features to a limited audience, testing at scale, and controlling the rollout process. You can easily manage feature flags within your Rails application with Flipper, and with its user interface, even non-technical users can make adjustments. By following the steps outlined in this article, you can quickly set up feature flags in your own application, allowing you to take full advantage of this powerful tool.

What to do next:
  1. Try Honeybadger for FREE
    Honeybadger helps you find and fix errors before your users can even report them. Get set up in minutes and check monitoring off your to-do list.
    Start free trial
    Easy 5-minute setup — No credit card required
  2. Get the Honeybadger newsletter
    Each month we share news, best practices, and stories from the DevOps & monitoring community—exclusively for developers like you.
    author photo

    Jeffery Morhous

    Jeff is a Software Engineer working in healtcare technology using Ruby on Rails, React, and plenty more tools. He loves making things that make life more interesting and learning as much he can on the way. In his spare time, he loves to play guitar, hike, and tinker with cars.

    More articles by Jeffery Morhous
    Stop wasting time manually checking logs for errors!

    Try the only application health monitoring tool that allows you to track application errors, uptime, and cron jobs in one simple platform.

    • Know when critical errors occur, and which customers are affected.
    • Respond instantly when your systems go down.
    • Improve the health of your systems over time.
    • Fix problems before your customers can report them!

    As developers ourselves, we hated wasting time tracking down errors—so we built the system we always wanted.

    Honeybadger tracks everything you need and nothing you don't, creating one simple solution to keep your application running and error free so you can do what you do best—release new code. Try it free and see for yourself.

    Start free trial
    Simple 5-minute setup — No credit card required

    Learn more

    "We've looked at a lot of error management systems. Honeybadger is head and shoulders above the rest and somehow gets better with every new release."
    — Michael Smith, Cofounder & CTO of YvesBlue

    Honeybadger is trusted by top companies like:

    “Everyone is in love with Honeybadger ... the UI is spot on.”
    Molly Struve, Sr. Site Reliability Engineer, Netflix
    Start free trial