CKEditor 未加载:有时在加载所需的 JS 文件之前执行代码

CKEditor not loading: Sometimes executes code before the needed JS file is loaded

我有两个工作视图页面(编辑和新建),其中包含一个我正在尝试添加 CKEditor 的文本区域。查看页面包括:

<%= f.text_area :page %>                 // This produces a textarea with id image_page

<script>
  CKEDITOR.replace( 'image_page' );      // I also tried wrapping this in `$(id='image_page').ready(function () {` `CKEDITOR.replace( 'image_page' );` `});` but that made no difference.
</script>

我在 application.html.erb 的 header 添加了 ckeditor.js:

<% if controller_name == 'images' && ['new', 'edit'].include?(action_name) %>
  <script src="/assets/ckeditor.js"></script>
<% end %>

自定义基本路径(所有文件都在 public/assets/ckeditor453/ 中)添加到 application.js。我在这里不需要 ckeditor.js 因为它已经包含在上面 header:

var CKEDITOR_BASEPATH = '/assets/ckeditor453/';

问题: 在另一个视图中通过 link 访问新视图或编辑视图时(非常标准的 links:例如 edit_image_path(image) ),然后在没有 ckeditor 的情况下加载新页面或编辑页面,并且出现 JS 错误 ReferenceError: CKEDITOR is not defined。刷新同一页面后,它会在存在 ckeditor 的情况下正确加载(错误因此消失)。
当直接访问新路径或编辑路径时(因此不是通过 link 而是在浏览器的地址栏中输入它们的地址),页面会立即正确加载,需要重新加载。

ckeditor.js 是否可能存在 JS loading/order 问题,因此当页面尝试调用 CKEDITOR.replace 时,ckeditor.js 尚未加载(至少在通过 link)?

访问页面时不会

有没有人理解这种行为并且可以深入了解我可以做些什么来解决它?

更新:

真的卡住了,不知道怎么办...

Github (https://github.com/tsechingho/ckeditor-rails/issues/28) 上的 post 描述了相同的行为并确认 turbolinks 是问题所在。对于使用 ckeditor 的 link 页面的 links,我禁用了 turbolinks。这解决了它:

link_to "Go", edit_image_path(image), data: {no_turbolink: true }