[英]Why does my rails app that uses Datatables not draw tables correctly when I use the back button, but does when I refresh the page?
I have built a rails app, using rails 5.2.3 and ruby 2.6.3 for browsing the user logs for another application and editing its users and their status.我已经构建了一个 rails 应用程序,使用 rails 5.2.3 和 ruby 2.6.3 来浏览另一个应用程序的用户日志并编辑其用户及其状态。 It's a simple app and I use rails Datatables to layout the tables.这是一个简单的应用程序,我使用 rails Datatables 来布置表格。 When I enter the url for the site, eg https://example.com/my_app/logs , it displays the log table with sorting, pagination, etc., just as it should.当我为该站点输入 url 时,例如https://example.com/my_app/logs ,它会按应有的方式显示带有排序、分页等的日志表。 If I move to my 'About' page, and from there click on the 'Back' link, it returns to the logs index view and shows the same url in the browser bar, BUT, the tables are not laid out using Datatables.如果我移动到我的“关于”页面,然后从那里单击“返回”链接,它会返回到日志索引视图并在浏览器栏中显示相同的 url,但是,表格不是使用 Datatables 布置的。 If I click the reload web page on the browser, it loads and draws correctly.如果我在浏览器上单击重新加载 web 页面,它会正确加载和绘制。 The back link on the About page is:关于页面的反向链接是:
<%= link_to 'Back', logs_path %>
And the logs_path is /logs而 logs_path 是 /logs
The application.js has: application.js 有:
//= require_self
//= require jquery
//= require jquery_ujs
//= require dataTables/jquery.dataTables
//= require dataTables/bootstrap/3/jquery.dataTables.bootstrap
//= require jquery.turbolinks
//= require turbolinks
//= require_tree .
The logs.coffee has: logs.coffee 有:
jQuery ->
$('#posts-table').dataTable()
sPaginationType: "full_numbers"
bJQueryUI: true
And the views/logs/index.html.erb has for the table:并且 views/logs/index.html.erb 具有该表:
<table id="posts-table" class="display" width="900">
So I guess I'm wondering, does the placement of the jQuery Datatable statement in the logs.coffee mean that it is not included when the link is executed?所以我想我想知道,在 logs.coffee 中放置 jQuery Datatable 语句是否意味着执行链接时不包含它? If not, then where should I put the dataTable() statement and what should it look like?如果没有,那么我应该把 dataTable() 语句放在哪里,它应该是什么样子?
Since you're using turbolinks, it looks like an issue with how turbolinks loads and caches the page.由于您使用的是 turbolinks,因此 turbolinks 如何加载和缓存页面似乎存在问题。
You could try placing your code inside a 'turbolinks:load' listener, like this:您可以尝试将代码放在“turbolinks:load”侦听器中,如下所示:
$(document).on 'turbolinks:load', () ->
$('#posts-table').dataTable()
sPaginationType: "full_numbers"
bJQueryUI: true
Anyway, you should consider how turbolinks handles the cached page, you can find more info on the matter in this question and in this article .无论如何,您应该考虑 turbolinks 如何处理缓存页面,您可以在此问题和本文中找到有关此问题的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.