本文介绍了如何将 Javascript 文件加载到 rails html erb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将如下 javascript 文件加载到我的 html.erb 文件中
Um trying to load a javascript file as follows to my html.erb file
<script src="/public/javascripts/test.js" type="text/javascript" />
它在公共文件夹和根目录中可用
its available on the public folder and its in the root directory
root/public/javascript
root/public/javascript
但它给了我一个错误说
"NetworkError: 404 Not Found - http://0.0.0.0:3000/public/javascripts/test.js"
可能有什么问题?有没有其他方法可以在 Rails 中包含 javascipt 文件?
what could possibly wrong ? is there a different way to include a javascipt file in rails ?
推荐答案
如果 javascript 文件位于 Rails 3.x 的任何资产文件夹(资产、供应商、捆绑包)中,您可以将脚本添加到您的 html.erb 文件,添加以下内容:
If the javascript file is in any of the asset folders (assets, vendor, bundle) for Rails 3.x) you can add the script to your html.erb file by adding the following:
<%= javascript_include_tag('test.js') %>
这篇关于如何将 Javascript 文件加载到 rails html erb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!