如何将Javascript文件加载到rails html erb

时间:2022-01-24 00:13:07

Um trying to load a javascript file as follows to my html.erb file

嗯尝试按如下方式将javascript文件加载到我的html.erb文件中

 <script src="/public/javascripts/test.js" type="text/javascript" /> 

its available on the public folder and its in the root directory

它在公用文件夹及其根目录中可用

root/public/javascript

but it giving me an error saying

但它给了我一个错误的说法

 "NetworkError: 404 Not Found  - http://0.0.0.0:3000/public/javascripts/test.js"

what could possibly wrong ? is there a different way to include a javascipt file in rails ?

什么可能错了?是否有一种不同的方式在rails中包含javascipt文件?

4 个解决方案

#1


21  

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文件位于Rails 3.x的任何资产文件夹(资产,供应商,软件包)中,则可以通过添加以下内容将脚本添加到html.erb文件中:

<%= javascript_include_tag('test.js') %>

#2


2  

Rails defaults to using the asset pipeline, so custom js files need to go into the app/assets/javascript directory. Than you wouldn't even need to load the file in your view.

Rails默认使用资产管道,因此自定义js文件需要进入app / assets / javascript目录。甚至不需要在视图中加载文件。

but as to your question.

但至于你的问题。

To serve the files from the public directory, you will need to enable a config setting

要从公共目录提供文件,您需要启用配置设置

config.serve_static_assets = true

You'd commonly put this in one of you environment config files.

您通常将其放在一个环境配置文件中。

#3


2  

You don't use public. Public is the implied root of the server.

你不使用公共。 Public是服务器的隐含根。

The server will look for a file in public, then try to route it through your router if it doesn't find a match.

服务器将公开查找文件,如果找不到匹配项,则尝试通过路由器进行路由。

You also may want to consider using the javascript_include_tag helper if you are using the asset pipeline.

如果您正在使用资产管道,您还可以考虑使用javascript_include_tag帮助程序。

#4


1  

To server files from public directory do above setting and hit

从公共目录服务器文件执行上面的设置并点击

config.serve_static_assets = true
<script src="/javascripts/test.js" type="text/javascript" /> 

#1


21  

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文件位于Rails 3.x的任何资产文件夹(资产,供应商,软件包)中,则可以通过添加以下内容将脚本添加到html.erb文件中:

<%= javascript_include_tag('test.js') %>

#2


2  

Rails defaults to using the asset pipeline, so custom js files need to go into the app/assets/javascript directory. Than you wouldn't even need to load the file in your view.

Rails默认使用资产管道,因此自定义js文件需要进入app / assets / javascript目录。甚至不需要在视图中加载文件。

but as to your question.

但至于你的问题。

To serve the files from the public directory, you will need to enable a config setting

要从公共目录提供文件,您需要启用配置设置

config.serve_static_assets = true

You'd commonly put this in one of you environment config files.

您通常将其放在一个环境配置文件中。

#3


2  

You don't use public. Public is the implied root of the server.

你不使用公共。 Public是服务器的隐含根。

The server will look for a file in public, then try to route it through your router if it doesn't find a match.

服务器将公开查找文件,如果找不到匹配项,则尝试通过路由器进行路由。

You also may want to consider using the javascript_include_tag helper if you are using the asset pipeline.

如果您正在使用资产管道,您还可以考虑使用javascript_include_tag帮助程序。

#4


1  

To server files from public directory do above setting and hit

从公共目录服务器文件执行上面的设置并点击

config.serve_static_assets = true
<script src="/javascripts/test.js" type="text/javascript" />