2014-04-25 67 views
1

环境:Ruby 2.0.0,Rails 4.1,Windows 8.1,Devise,jquery-datatables-rails 1.12.2。我也在使用行为作为租客,这是一个美妙的多租户宝石。jquery-datatables-rails gem在页面刷新前无法正确显示

除了一个小问题,我已经将datatables gem启动并运行了。当从索引操作访问页面时,该页面未被格式化并正在工作。只有浏览器刷新才会启动,然后才能正常工作。由于它在刷新后工作,在我看来,宝石设置是好的。

我尝试了多个修复程序。我最小化了页面,以便只显示表格。我尝试了IE和Chrome。无论是否已清除浏览器缓存和/或重新启动应用程序,都会发生这种情况。我检查了HTML以确保我没有看到任何问题。我查看了服务器日志,发现其中没有差异。我所做的任何事似乎都没有改变。

这里是之前和意见后,表示在访问页面时再刷新:

首先访问:

First access

刷新之后,一切工作正常:

After refresh

索引行为是基本的,但请注意它的作用范围为租户:

def index 
    @devices = Device.all 
    @roles = Role.all 
    end 

index.html.erb是:

<div class="row"> 
    <%= render partial: 'index', layout: 'layouts/sf_label', locals: { title: 'List Devices' } %> 
</div> 

局部_index.html.erb是:

<div class="span8"> 
    <table id="datatable"> 
    <thead> 
    <tr> 
     <th>Device</th> 
     <th>Created</th> 
     <th>Role</th> 
    </tr> 
    </thead> 
    <tbody> 
    <% @devices.each do |device| %> 
     <tr> 
      <td><%= link_to device.name, edit_device_path(device.id) %></td> 
      <td><%= device.created_at.to_date %></td> 
      <td><%= device.roles.first.name.titleize unless device.roles.first.nil? %></td> 
     </tr> 
    <% end %> 
    </tbody> 
    </table> 
</div> 

在Gemfile中:

gem 'jquery-datatables-rails', github: 'rweng/jquery-datatables-rails' 
gem 'jquery-ui-rails' 

在应用程序。 js:

//= require dataTables/jquery.dataTables 
//= require dataTables/jquery.dataTables.bootstrap 

在datatable.js.coffee:

jQuery -> 
    $('#datatable').dataTable() 

在devices.js.coffee:

$('.datatable').dataTable({ 
    "sPaginationType": "bootstrap" 
}); 

在application.css.css:

*= require dataTables/jquery.dataTables 
*= require dataTables/jquery.dataTables.bootstrap 

在日志中,这是相同的任一方式:

Rendered devices/_index.html.erb (253.2ms) 
    Rendered devices/index.html.erb within layouts/application (254.2ms) 
    Rendered layouts/_shim.html.erb (1.0ms) 
    CACHE (0.0ms) SELECT COUNT(*) FROM "roles" INNER JOIN "devices_roles" ON "roles"."id" = "devices_roles"."role_id" WHERE "devices_roles"."device_id" = $1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL))) [["device_id", 51]] 
    Rendered layouts/_navigation_links.html.erb (3.0ms) 
    Rendered layouts/_navigation.html.erb (4.0ms) 
    Rendered layouts/_messages.html.erb (1.0ms) 
Completed 200 OK in 290ms (Views: 248.2ms | ActiveRecord: 36.0ms) 

感谢所有的意见...

回答

10

尝试这一次,

添加turbolinks宝石到宝石文件>>

gem 'turbolinks' 
gem 'jquery-turbolinks' 

并添加以下内容的application.js文件。

//= require jquery.turbolinks 
//= require turbolinks 

打包并重新启动服务器。

+0

完美!谢谢!我应该补充说我在那里有turbolinks,我只需要在两个位置添加jquery版本,FWIW。 –

+0

我欢迎... –

+0

出于好奇,似乎通过阅读文件,我有选择要么删除turbolinks或添加jquery-turbolinks。 turbolinks打破了一些jquery的功能,jquery-turbolinks解决了这些问题。有趣。 –