您没有通过在视图中编写脚本来使用 Rails 资产管道。如果您查看rail guides,它会说
Asset pipeline concatenate assets, which can reduce the number of requests that a browser makes to render a web page. Web browsers are limited in the number of requests that they can make in parallel, so fewer requests can mean faster loading for your application.
所以你应该使用它。
你可以做一个单独的文件让我们说 app/assets/javascript 目录中的 update_details.js 然后在 application.js 中要求该文件
//= require update_details
或者如果你有 //= require_tree 。在 application.js 中,它会自动加载 app/assets/javascript 文件夹中的所有 js 文件,因此您不需要单独要求 update_details。之后,您可以使用
在布局中简单地链接 application.js
<%= javascript_include_tag "application" %>
或
如果您只想加载 update_details 并保留所有其他 js 文件,那么您可以使用
<%= javascript_include_tag "update_details" %>
在你的布局中,也不要忘记将它包含在你的资产预编译路径中
Rails.application.config.assets.precompile += %w( jquery.inview.min.js update_details.js )