4

我正在关注rails casts#340以在我的应用程序中实现数据表。找不到文件'dataTables/jquery.dataTables'Rails 4

以下所有步骤,我得到这个错误: 找不到文件“数据表/ jquery.dataTables”

我使用的轨道4,我发现了一些教程,我不得不提到的文件创建像application.css和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 . 
*/ 

并且是轨道4我已经添加了调用样式表在/layouts/application.html.erb

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

和产品/ 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> 

我得到这个错误在输出

找不到文件'dataTables/jquery.dataTables' (in .... app/assets/stylesheets/application.css:6)

任何想法如何解决这个问题? 在此先感谢

+0

我采取了简单的方法,将css和js从gem直接复制到资产/样式表和资产/ javascript文件夹中......我对“无法找到文件”错误没有多少耐心! – Danny

回答

2

看看这个related question。我最近碰到类似的问题,我的发现可能会帮助你。

我看到你提到你必须创建一个application.css文件,但是你没有指出是否用任何东西填充它。你可以尝试添加:

*= require jquery.dataTables 

application.css和包括的.app /资产/数据表的jquery.dataTables.css副本,然后让你的能够样式数据表到您的应用程序的其他部分相匹配的额外的奖励。

进行这些更改将解决“无法找到文件”错误,因为Rails正在查找一个CSS文件,该文件或者:a)不存在;或者b)不存在于您的application.css文件中指定的位置。

另外,您不应该在您的application.html.erb文件中添加stylesheet_link_tag。资产管道的全部内容是turbolinks会根据您在application.css中指定的内容将资产(如样式表)添加到您的页面。

+0

谢谢 - 但我似乎没有任何东西在我的.app/assets/datatables目录 - 关于它发生了什么的任何线索?我的Datatable似乎工作正常,但是当我运行rspec时,它给了我这些错误 – BKSpurgeon

+0

@BKSpurgeon您可能希望将自己的半相关但不同的问题作为一个问题发布,而不是将它隐藏在评论中两年半的答案。我可以告诉你到底发生了什么,但我宁愿这样做,以便后续用户可以在他们所期望的位置找到答案。 – MarsAtomic

+0

谢谢。对于未来的读者 - 找到答案 - 我运行rspec并将相关的gem添加到测试环境:问题解决了! – BKSpurgeon

6

Rails 4中没有更多资产组,因此只需从该块中获取宝石。

+0

删除:资产组,我仍然收到错误 – LeoJ

+0

对不起忘了重启服务器,但我仍然看不到在列 – LeoJ

+0

列中的搜索框和订单选项好了,这样问题就出现了。关于搜索框和其他内容 - 请粘贴您用来初始化数据表的代码。 – Almaron