Search code examples
ruby-on-railsruby-on-rails-4datatablesjquery-datatables-rails

jquery-datatables-rails gem not applying to table


I'm using rails 4.2.4 and after generating a basic product scaffold I cant seem to get the gem jquery-datatables-rails. I'm using the github readme along with the railscast episode to try and get it working with no luck at all.

http://railscasts.com/episodes/340-datatables

https://github.com/rweng/jquery-datatables-rails

Gemfile

gem 'jquery-datatables-rails', '~> 3.3.0'

application.js

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

jQuery -> $('.datatable').DataTable();

application.css

*= require dataTables/jquery.dataTables
*= require_tree .
*= require_self

index.html.erb

<table class="datatable">
  <thead>
    <tr>
      <th>Name</th>
      <th>Price</th>      
    </tr>
  </thead>

  <tbody>
    <% @products.each do |product| %>
      <tr>
        <td><%= product.name %></td>
        <td><%= product.price %></td>
      </tr>
    <% end %>
  </tbody>
</table>

I have tried directly referencing the github repo in my gemfile. The generate install doesn't seem add the requires in properly either way so I have put them in manually.


Solution

  • Managed to fix this by putting this script into the view

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