DataTable无法在rails app中运行

时间:2016-02-23 12:32:32

标签: ruby-on-rails-4 datatables

我已正确编写代码,但我的Datatable无法正常工作



// 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 any plugin's vendor/assets/javascripts directory 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
// compiled file.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//= require bootstrap
//= require jquery
//= require jquery_ujs
//= require dataTables/bootstrap/3/jquery.dataTables.bootstrap
//= require turbolinks
//= require dataTables/jquery.dataTables
//= require_tree .

@import url("https://fonts.googleapis.com/css?family=Open+Sans:400,700");/*!
 * bootswatch v3.3.6
 * Homepage: http://bootswatch.com
 * Copyright 2012-2016 Thomas Park
 * Licensed under MIT
 * Based on Bootstrap
*//*!
 * Bootstrap v3.3.6 (http://getbootstrap.com)
 * Copyright 2011-2015 Twitter, Inc.
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
@import url("https://fonts.googleapis.com/css?family=Open+Sans:400,700");/*!
/*
 * 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 any plugin's vendor/assets/stylesheets directory 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 bottom of the
 * compiled file so the styles you add here take precedence over styles defined in any styles
 * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
 * file per style scope.
 *= require dataTables/bootstrap/3/jquery.dataTables.bootstrap
 *= require_self
 *= require dataTables/jquery.dataTables
 *= require_tree .
 */

<div class="table-responsive">
  <table class="table" id="users" >
    <thead>
    <tr>
      <th>Name</th>
      <th>Email</th>
      <th>Change Type</th>
      <th>Action</th>
      <th>Delete</th>
    </tr>
    </thead>
    <tbody>
      <% @users.each do |user| %>
        <tr>
          <td><%= user.name %></td>
          <td>
            <%= link_to user.email, user %>
          </td>
          <td>
            <%= form_for(user) do |f| %>
              <%= f.select(:role, User.roles.keys.map {|role| [role.titleize,role]}) %>
              <td>
              <button><%= f.submit 'Change Role' %></button>
              </td>
            <% end %>
          </td>
          <td>
            <%= link_to("Delete user", user_path(user), data: { confirm: "Are you sure?" }, method: :delete, class: 'button') unless user == current_user %>
          </td>
        </tr>
      <% end %>
    </tbody>
  </table>
</div>

<script>
  $('#users').dataTable();
</script>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

将脚本更改为:

<script>
$(document).ready(function() {
    $('#users').dataTable();
} );
</script>