我将 DataTables 与我的 Rails 应用程序一起使用。
出于某种原因,DataTables/JQuery 似乎认为我正在尝试重新初始化 DataTable。每当我加载使用 DataTables 的页面时,都会出现以下错误:
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
为什么会这样?据我所知,我的页面只加载一次脚本,我知道它每页只生成一个表格元素......
/application/views/layouts/application.html.haml:
!!!
%html
%head
= javascript_include_tag "application"
%body
= yield
/application/views/reports/index.html.haml:
%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)
我很确定我已经正确设置了资产管道......
/assets/javascripts/application.js:
//= require jquery
//= require jquery_ujs
//= require dataTables/jquery.dataTables
//= require html5shiv
//= require_tree .
而且我很确定我正确地调用了 DataTables ......
/assets/javascripts/tables.js.coffee:
jQuery ->
$('table').dataTable
sPaginationType: "full_numbers"
bJQueryUI: true
而且我认为我的任何宝石都不会导致这种行为:
宝石文件:
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
更新:我知道这与资产管道有关,因为 Heroku 上生产中的相同代码(之后rake assets:precompile
)不会导致它在开发中引起的问题(应用程序为我即时组装资产) .)