为什么DataTables不能在我的rails中工作?

时间:2012-07-12 18:10:09

标签: javascript ruby-on-rails datatables partial

我的rails应用程序中有一个部分表,我想使用DataTables使它看起来不错。我正在使用jquery-datatables-rails gem。

我的部分:

 <script type="text/javascript">
    $(document).ready(function() {
    $('#businesses').dataTable();
    } );
    </script>

<table id="businesses">
            <thead>
                <tr>
                            Bunch of headers here...

                </tr>
                </thead>

                <tbody>
                <tr>
                Lots and lots of code here...
                </tr>
            </tbody>
        </table>

的application.js:

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require dataTables/jquery.dataTables
//= require_tree .

application.css:

*
 * This is a manifest file that'll be compiled into application.css, which will include all the files
 * listed below.
 *
 * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
 * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
 *
 * You're free to add application-wide styles to this file and they'll appear at the top of the
 * compiled file, but it's generally better to create a new file per style scope.
 *
 *= require_self
 *= require dataTables/jquery.dataTables
 *= require_tree .
 */

DataTables文件在application.jsapplication.css中也被正确引用。

谢谢!

1 个答案:

答案 0 :(得分:0)

您应该将js代码移到application.js文件中。

所以,你的html.erb中没有脚本标签来做javascript部分你应该有一个像这样的application.js:

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require dataTables/jquery.dataTables
//= require_tree .

$('#businesses').dataTable();
    } );