Rails integration guide

Add Bugsnag to your Ruby on Rails projects.

Installation

Add the bugsnag Ruby gem to your Gemfile:

gem 'bugsnag'

Don’t forget to run bundle install after updating your Gemfile.

Basic configuration

Run our generator to create the config/initializers/bugsnag.rb configuration file and set your project’s integration API key:

$ rails generate bugsnag YOUR_API_KEY_HERE

Alternatively, you can set the BUGSNAG_API_KEY environment variable.

You can find your integration API key immediately after creating a new project from your Bugsnag dashboard, or later on your project’s settings page.

Further configuration

If you’d like to configure Bugsnag further, check out the configuration options reference.

Reporting unhandled exceptions

After completing installation and basic configuration, unhandled exceptions in your Rails app will be automatically reported to your Bugsnag dashboard.

Exceptions in Sidekiq, Resque, Delayed Job, Mailman, and Rake will also be automatically reported.

Reporting handled exceptions

Reporting handled exceptions can be accomplished as follows:

begin
  raise 'Something went wrong!'
rescue => exception
  Bugsnag.notify(exception)
end

Adding diagnostics or adjusting severity

It can often be helpful to adjust the severity or attach custom diagnostics to handled exceptions. For more information, see reporting handled errors.

Sending diagnostic data

Automatically captured diagnostics

Bugsnag will automatically capture and attach the following diagnostic data to every exception report:

  • A full stacktrace
  • Request information, including ip, headers, URL, HTTP method, and HTTP params
  • Session data
  • Release stage (production, beta, staging, etc)
  • Hostname

Attaching custom diagnostics

It can often be helpful to attach application-specific diagnostic data to exception reports. This can be accomplished as follows:

class ApplicationController < ActionController::Base
  before_bugsnag_notify :add_diagnostics_to_bugsnag

  # Your controller code here

  private
  def add_diagnostics_to_bugsnag(notification)
    notification.add_tab(:diagnostics, {
      product: current_product.name
    })
  end
end

For more information, see customizing error reports.

Identifying users

In order to correlate errors with customer reports, or to see a list of users who experienced each error, it is helpful to capture and display user information:

If you are using the Devise, Warden, or Clearance authentication frameworks, we will automatically capture information about the currently authenticated user. Otherwise, you can set this information as follows:

class ApplicationController < ActionController::Base
  before_bugsnag_notify :add_user_info_to_bugsnag

  # Your controller code here

  private
  def add_user_info_to_bugsnag(notification)
    notification.user = {
      email: current_user.email,
      name: current_user.name,
      id: current_user.id
    }
  end
end

For more information, see customizing error reports.

Tracking deploys

By sending your source revision or application version to us when you deploy a new version of your app, you’ll be able to see which deploy each error was introduced in, or seen in.

If you are using Capistrano, notify Bugsnag of your deploys by adding our integration to your Capfile:

require 'bugsnag/capistrano'
set :bugsnag_api_key, 'api_key_here'

If you are using a deployment system other than Capistrano, see our deploy tracking guide.

Catching JavaScript errors

Catch and report errors in your client-side JavaScript with the Web browser JavaScript guide