简体   繁体   English

用Rails加载JavaScript文件

[英]Loading JavaScript File With Rails

So running Rails 3.2. 因此运行Rails 3.2。 In my assets/javascripts folder I have the file " main.js.erb ". 在我的assets/javascripts文件夹中,文件“ main.js.erb ”。 The contents of this file are as follows: 该文件的内容如下:

$(function(){
  $(document).load(alert("Hello"))
})

My application.js file is just the default generated after an install initiated with ' rails new '. 我的application.js文件只是使用“ rails new ”启动安装后生成的默认文件。

Any ideas as to why the load event is not being triggered? 关于为何未触发load事件的任何想法?

Since your application.js is just the default, you probably haven't included the file for usage. 由于您的application.js只是默认设置,因此您可能没有包含该文件以供使用。

Following are different ways you can include the file: 以下是包含文件的不同方法:

  1. In app/assets/javscripts/application.js app/assets/javscripts/application.js

     //= require main 
  2. In your template file or layout file eg. 在您的模板文件或布局文件中。 app/views/layouts/application.html.erb app / views / layouts / application.html.erb

     <%= javascript_include_tag "main" %> 

For further details please refer to The Asset Pipeline RailsGuides . 有关更多详细信息,请参阅《资产管道RailsGuides》

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM