0

Railscast之后,我有了 DataTables,一个 JQuery 插件,在我的 Rails 3.2.9 应用程序中运行良好。

然后它突然停止工作。(也就是说,表格现在只是呈现为纯 HTML。)而且我不知道是什么更改导致了损坏,我无法弄清楚发生了什么。)你可以吗?

JQuery 似乎可以正常加载,因为我存储在 中的这个脚本/app/assets/javascripts/nospam.js正在执行它在 DataTables 失败的同一页面上应该执行的操作:

$(window).load(function() {
  $('#my-email').html(function(){
    var e = "steven";
    var a = "@";
    var d = "testivate";
    var c = ".com";
    var h = 'mailto:' + e + a + d + c;
    $(this).parent('a').attr('href', h);
    return e + a + d + c;
  });
});

当我在 Chrome 中加载页面并转到查看 > 开发人员 > JavaScript 控制台 > 控制台时,我看到Uncaught TypeError: Object [object Object] has no method 'dataTable'连接到return包含脚本中的行的错误:

(function() {
  jQuery(function() {
    return $('#areas').dataTable({
      bJQueryUI: true
    });
  });
}).call(this);

资产管道似乎正在加载所有正确的文件,因为它生成:

<!DOCTYPE html>
<html>
  <head>
    <title>Testivate</title>
    <!--[if lt IE 9]>
      <script src="/assets/html5shiv-printshiv.js?body=1" type="text/javascript"></script>
    <![endif]-->
    <script src="/assets/jquery.js?body=1" type="text/javascript"></script>
    <script src="/assets/jquery_ujs.js?body=1" type="text/javascript"></script>
    <script src="/assets/dataTables/jquery.dataTables.js?body=1" type="text/javascript"></script>
    <script src="/assets/areas.js?body=1" type="text/javascript"></script>
    <script src="/assets/assignments.js?body=1" type="text/javascript"></script>
    <script src="/assets/categories.js?body=1" type="text/javascript"></script>
    <script src="/assets/heuristics.js?body=1" type="text/javascript"></script>
    <script src="/assets/jquery.placeholder.min.js?body=1" type="text/javascript"></script>
    <script src="/assets/nospam.js?body=1" type="text/javascript"></script>
    <script src="/assets/reports.js?body=1" type="text/javascript"></script>
    <script src="/assets/application.js?body=1" type="text/javascript"></script>
    <link href="/assets/application.css?body=1" media="screen" rel="stylesheet" type="text/css" />
    <link href="/assets/jquery.ui.core.css?body=1" media="screen" rel="stylesheet" type="text/css" />
    <link href="/assets/jquery.ui.theme.css?body=1" media="screen" rel="stylesheet" type="text/css" />
    <link href="/assets/dataTables/src/demo_table_jui.css?body=1" media="screen" rel="stylesheet" type="text/css" />
    <link href="/assets/_base.css?body=1" media="screen" rel="stylesheet" type="text/css" />
    <link href="/assets/ie.css?body=1" media="screen" rel="stylesheet" type="text/css" />
    <link href="/assets/ie6.css?body=1" media="screen" rel="stylesheet" type="text/css" />
    <link href="/assets/print.css?body=1" media="screen" rel="stylesheet" type="text/css" />
    <link href="/assets/screen.css?body=1" media="screen" rel="stylesheet" type="text/css" />
    <link href="http://fonts.googleapis.com/css?family=Rokkitt:400,700" media="screen" rel="stylesheet" type="text/css" />
    <link href="http://fonts.googleapis.com/css?family=Open+Sans:400italic,400" media="screen" rel="stylesheet" type="text/css" />
    <meta content="authenticity_token" name="csrf-param" />
    <meta content="Rss5Hf9sPcQQQRViXgEUfSxvYEshBukjV96gM5+cEYc=" name="csrf-token" />
    <meta content='jq8ezzUss-fo-h7ml8zmwSvdpmzJlbE6VjN7Dphcnp8' name='google-site-verification'>
  </head>

从其他相关文件中选择的代码...

/app/assets/javascripts/application.js

//= require jquery
//= require jquery_ujs
//= require dataTables/jquery.dataTables
//= require_tree .

/app/assets/javascripts/areas.js.coffee

jQuery ->
  $('#areas').dataTable
    bJQueryUI: true

/app/views/areas/index.html.haml

%table.display#areas
  %thead
    %tr
      %td Foo
      %td Bar
  %tbody
    %tr
      %td Foo
      %td Bar

宝石文件

gem "compass"
gem "sass"
gem "sass-rails"
gem "haml"
gem "haml-rails"
gem "susy"
gem "jquery-rails"
gem "html5shiv-rails"
gem "normalize-rails"

group :assets do
  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

/config/application.rb

if defined?(Bundler)
  Bundler.require *Rails.groups(:assets => %w(development test))
end

module Testivate
  class Application < Rails::Application
    config.assets.enabled = true
    config.assets.version = '1.0'
  end
end

/config/environments/development.rb

Testivate::Application.configure do
  config.assets.compress = false
  config.assets.debug = true
end
4

2 回答 2

0

问题确实出在资产管道上。它正在加载 JQuery 的两个副本,因为我已经预编译了资产以将它们推送到生产环境,但是预编译的版本在本地徘徊并与动态编译的版本一起提供。现在要弄清楚如何阻止这种情况的发生......

于 2012-12-14T04:34:17.613 回答
0

在 Chrome 中使用相同的开发人员工具。单击网络并检查 /assets/dataTables/jquery.dataTables.js?body=1 是否正在加载。

于 2012-12-14T03:44:50.580 回答