4

I'm following the rails casts #340 to implement data tables in my application.

Following all the steps I get this error: couldn't find file 'dataTables/jquery.dataTables'

I'm using rails 4 and I found that some files that mentions in the tutorial I had to create like application.css and products.js.coffee

Gemfile

gem 'jquery-rails'
group :assets do 
  gem 'jquery-datatables-rails', github: 'rweng/jquery-datatables-rails'
  gem 'jquery-ui-rails'
end

Application.js

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

Application.js.coffee

 /* 
 * This is a manifest file that'll automatically include all the stylesheets available in this directory 
 * and any sub-directories. 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 .  
*/  

and as is rails 4 I've added the call to the stylesheet in the /layouts/application.html.erb

<%= stylesheet_link_tag 'application.css', media: 'all' %>

and the products/index.htm.erb

<table class="table table-normal" id="products" >
  <thead>
    <tr>
                      <td>Code</td>
                      <td>Name</td>
                      <td>Description</td>
                      <td>Price</td>
                      <td>Stock</td>
                      <td>Enabled</td>
                      <td>Company</td>
              </tr>

  </thead>

  <tbody>   

    <% @products.each do |product| %>
      <tr>                          
            <td style="text-decoration: underline;"><%= link_to product.code, edit_product_path(product) %></td>             

            <td><%= product.name %></td>             

            <td><%= product.description %></td>              

            <td><%= product.price %></td>              

            <td><%= product.stock %></td>              

            <td><%= product.enabled %></td>              

            <td><%= product.company_id %></td>              

      </tr>
    <% end %>
  </tbody>
</table>

I get this error in the output

couldn't find file 'dataTables/jquery.dataTables' (in ....app/assets/stylesheets/application.css:6)

Any ideas how to solve this? Thanks in advance

LeoJ
  • 57
  • 2
  • 6
  • I took the easy way out and copied the css and js from the gem directly into my assets/stylesheets and assets/javascript folders...I have little patience for the "couldn't find file" errors! – Danny Feb 22 '14 at 22:38

2 Answers2

6

There's no more :assets group in Rails 4, so just get the gems out of that block.

Almaron
  • 4,127
  • 6
  • 26
  • 48
  • Sorry forgot to restart the server, but still I cant see the search box and the order options in the columns – LeoJ Nov 01 '13 at 15:59
  • Ok, so that problem is down. About the searchbox and the other stuff - please paste in the code you use to initialize the datatable. – Almaron Nov 01 '13 at 16:01
  • In the /products/index.html.erb `">` and the initializer in /javascripts/products.js `jQuery -> $('#products').dataTable()`
    – LeoJ Nov 01 '13 at 16:26
  • the same as is in the rail cast – LeoJ Nov 01 '13 at 16:26
2

Take a look at this related question. I ran into a similar issue recently, and my findings may help you out.

I saw that you mentioned you had to create an application.css file, but you didn't indicate whether you populated it with anything. You could try adding:

*= require jquery.dataTables

to application.css and including a copy of jquery.dataTables.css at .app/assets/datatables, which then gives you the added bonus of being able to style DataTables to match the rest of your app.

Making these changes will resolve the "couldn't find file" error, since Rails is looking for a CSS file that either: a) does not exist; or b) does not exist at the location specified in your application.css file.

Also, you should not be adding a stylesheet_link_tag to your application.html.erb file. The whole point of the asset pipeline is that turbolinks will add assets (like stylesheets) to your pages based on what you've specified in your application.css.

Community
  • 1
  • 1
MarsAtomic
  • 10,436
  • 5
  • 35
  • 56
  • thank you - but I don't seem to have anything in my .app/assets/datatables directory - any clue as to what happened to it? My Datatable seems to be working fine, but when I run rspec, it's giving me these errors – BenKoshy Jul 03 '16 at 10:55
  • @BKSpurgeon You probably want to post your semi-related but different question as a question on its own, rather than hiding it in a comment to a two and-a-half year old answer. I can tell you exactly what's going on, but I'd rather do it in a way that allows subsequent users to find answers where they expect them. – MarsAtomic Jul 03 '16 at 22:55
  • thank you. For those reading in future - found the answer - i was running rspec and added the relevant gems to the testing environment: problem solved! – BenKoshy Jul 03 '16 at 23:00