How to use Flipper for feature flags in Rails
Tags: ruby, rails, tutorial
Published: 2023-09-28 23:04:22

*This article was originally written by [Jeffery Morhous](https://www.honeybadger.io/blog/authors/jefferymorhous/) on the [Honeybadger Developer Blog](https://www.honeybadger.io/blog/flipper-feature-flags-rails/).*

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](https://github.com/jnunemaker/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:

```ruby
if rand < 0.50
  redirect_to_beta_interface
else
  # Handle request normally
end

def redirect_to_beta_interface
  # Redirect code would go here
end
```

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](https://github.com/jnunemaker/flipper) 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:

```ruby
gem 'flipper'
gem 'flipper-ui'
gem 'flipper-active_record'
```

Next, run the following:

```bash
bundle install
```

Next, generate Flipper's required migrations:

```bash
bin/rails g flipper:active_record
```

Finally, run the generated migrations:

```bash
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:

```ruby
if Flipper.enabled?(:beta_interface)
  redirect_to_beta_interface
else
  # Handle request normally
end

def redirect_to_beta_interface
  # Redirect code would go here
end
```

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:

```ruby
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)
end
```

## 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:

```ruby
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](https://www.flippercloud.io/docs/getting-started)
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:

```ruby
mount Flipper::UI.app(Flipper) => '/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](https://www.flippercloud.io/docs/ui) 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.

## Conclusion

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.