This simple script takes screenshot on every test failure and saves it in tmp/rspec_screens directory:

# -*- encoding : utf-8 -*-
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rspec'
require 'capybara/poltergeist'

Capybara.javascript_driver = :poltergeist
SCREENSHOT_DIR = Rails.root.join("tmp/rspec_screens")

def take_screenshot(example)
  meta            = example.metadata
  filename         = File.basename(meta[:file_path])
  line_number     = meta[:line_number]
  screenshot_name = "screenshot-#{filename}-#{line_number}.png"
  screenshot_path = File.join(SCREENSHOT_DIR, screenshot_name)
  
  page.save_screenshot(screenshot_path)
  
  puts meta[:full_description] + "\n  Screenshot: #{screenshot_path}"
end

RSpec.configure do |config|
 
  config.before(:suite) do
    SCREENSHOT_DIR = Rails.root.join("tmp/rspec_screens")
    FileUtils.rm_rf(SCREENSHOT_DIR)
    FileUtils.mkdir_p(SCREENSHOT_DIR)
  end

  config.after(:each) do |example|
    if example.metadata[:type] == :feature and example.exception.present?
      take_screenshot(example)
    end
  end
end