Ruby on Rails Integration Guide

Ruby Feature Flags

Feature flags for Ruby on Rails — safe rollouts with simple HTTP integration

Quick Start

1. Get your API key

Sign up at flagbit.anethoth.com to get your SDK key. The free tier includes 1,000 evaluations/day.

2. Install (optional)

gem install httparty

3. Evaluate flags

require 'httparty'
require 'json'

module FlagBit
  SDK_KEY = ENV['FLAGBIT_SDK_KEY']
  URL = 'https://flagbit.anethoth.com/api/v1/evaluate'

  def self.enabled?(flag_key, context = {})
    resp = HTTParty.post(URL,
      headers: { 'Content-Type' => 'application/json', 'X-SDK-Key' => SDK_KEY },
      body: { flag_key: flag_key, context: context }.to_json,
      timeout: 5
    )
    resp.parsed_response['value'] == true
  rescue StandardError
    false
  end
end

# Rails controller concern
module FeatureFlags
  extend ActiveSupport::Concern

  private

  def feature_enabled?(flag_key)
    context = current_user ? { user_id: current_user.id.to_s, plan: current_user.plan } : {}
    FlagBit.enabled?(flag_key, context)
  end

  def require_feature!(flag_key)
    head :not_found unless feature_enabled?(flag_key)
  end
end

# Controller usage
class DashboardController < ApplicationController
  include FeatureFlags
  before_action -> { require_feature!('new-dashboard') }, only: [:beta]

  def index
    if feature_enabled?('analytics-v2')
      render :index_v2
    else
      render :index
    end
  end
end

# View helper
module ApplicationHelper
  def feature_flag(flag_key, context = {}, &block)
    capture(&block) if FlagBit.enabled?(flag_key, context)
  end
end

# In ERB: <%= feature_flag('promo-banner') { render 'promo' } %>

Use Cases for Ruby on Rails

Rails Migrations

Gate new Active Record models or controllers behind flags during schema migrations.

Mailer Experiments

A/B test email templates by flagging which version of ActionMailer template to use.

Sidekiq Jobs

Enable or disable background jobs without redeploying — useful during incidents.

Multi-tenant Features

Enable premium features per tenant using targeting rules based on organization ID.

How FlagBit Works

1

Create a Flag

Define flags in your FlagBit dashboard with targeting rules and rollout percentages.

2

Evaluate in Code

Call the evaluate endpoint from your Ruby on Rails app with user context for targeted rollouts.

3

Toggle Instantly

Enable, disable, or adjust rollouts in real-time. No redeployment needed.

FAQ

FlagBit is a hosted API — no database tables in your Rails app, no gem dependencies, no self-hosting. Trade-off: network call vs. local DB query.

Yes — the view helper above lets you wrap any ERB block in a feature flag check.

Stub FlagBit.enabled? in RSpec: allow(FlagBit).to receive(:enabled?).with('flag-key').and_return(true)

Use Rails.cache.fetch with a TTL: Rails.cache.fetch("flag:#{key}", expires_in: 1.minute) { FlagBit.enabled?(key, ctx) }

Start using feature flags in Ruby on Rails

Free tier includes 1 project, 10 flags, and 1,000 evaluations/day. No credit card required.

Get Your Free API Key