title: DataTables in Rails -- how do get rid of "Cannot reinitialise DataTable" error
body: <p>I am using DataTables with my Rails app.</p>
  
  <p>For some reason, DataTables/JQuery seems to think I'm trying to reinitialise DataTable. Whenever I load a page that uses DataTables, it gives this error:</p>
  
  <pre><code>DataTables warning (table id = 'DataTables_Table_0'): Cannot reinitialise DataTable.
  To retrieve the DataTables object for this table, pass no arguments or see the docs for bRetrieve and bDestroy
  </code></pre>
  
  <p>Why is this happening? My pages only load the script once as far as I can see and I know that it generates just one table element per page...</p>
  
  <p><strong>/application/views/layouts/application.html.haml</strong>:</p>
  
  <pre><code>!!!
  %html
    %head
      = javascript_include_tag "application"
    %body
      = yield
  </code></pre>
  
  <p><strong>/application/views/reports/index.html.haml</strong>:</p>
  
  <pre><code>%table
    %thead
      %tr
        %th Short Name
        %th Controls
    %tbody
      - @reports.each do |report|
        %tr
          %td= link_to report.full_name, report_path(report)
          %td
            = link_to "Edit", edit_report_path(report)
            \|
            = delete_link "Delete", report_path(report)
  </code></pre>
  
  <p>I'm pretty sure I've set up the asset pipeline correctly...</p>
  
  <p><strong>/assets/javascripts/application.js</strong>:</p>
  
  <pre><code>//= require jquery
  //= require jquery_ujs
  //= require dataTables/jquery.dataTables
  //= require html5shiv
  //= require_tree .
  </code></pre>
  
  <p>And I'm pretty sure I'm calling DataTables correctly...</p>
  
  <p><strong>/assets/javascripts/tables.js.coffee</strong>:</p>
  
  <pre><code>jQuery -&gt;
    $('table').dataTable
      sPaginationType: "full_numbers"
      bJQueryUI: true
  </code></pre>
  
  <p>And I don't think any of my gems will be causing the behaviour:</p>
  
  <p><strong>Gemfile</strong>:</p>
  
  <pre><code>source "http://rubygems.org"
  
  gem "rails", "3.2.11"
  gem "activesupport"
  gem "actionmailer"
  gem "mail"
  gem "railties"
  gem "compass"
  gem "pg"
  gem "sass"
  gem "sass-rails"
  gem "haml"
  gem "haml-rails"
  gem "susy"
  gem "jquery-rails"
  gem "google-analytics-rails"
  gem "declarative_authorization"
  gem "authlogic"
  gem "simple_form"
  gem "country-select"
  gem "rails3-generators"
  gem "hirb"
  gem "debugger"
  gem "redis"
  gem "RedCloth"
  gem "friendly_id"
  gem "high_voltage"
  gem "validatious"
  gem "houdini"
  gem "rspec-rerun"
  gem "html5shiv-rails"
  gem "normalize-rails"
  gem "simple-navigation"
  gem "thin"
  
  group :development do
    gem "taps"
    gem "better_errors"
    gem "binding_of_caller"
  end
  
  group :test do
    gem "capybara"
    gem "rspec-rails"
    gem "database_cleaner"
    gem "launchy"
    gem "spork", "0.9.2"
    gem "respec"
  end
  
  group :assets do
    gem "better_errors"
    gem "sass-rails"
    gem "coffee-rails"
    gem "compass-rails"
    gem "compass-susy-plugin"
    gem "fancy-buttons"
    gem 'jquery-datatables-rails', github: 'rweng/jquery-datatables-rails'
    gem "jquery-ui-rails"
    gem "uglifier"
  end
  </code></pre>
  
  <p><strong>UPDATE</strong>: I know this has <em>something</em> to do with the asset pipeline, because the same code in production on Heroku (after <code>rake assets:precompile</code>) is not causing the problem that it is causing here on development (where the app assembles assets for me on-the-fly.)</p>