我使用rails和Javascript的经验非常有限。
我正在尝试弹出一个基本的警报功能,以确保一切都在工作状态,但它不工作,因此,事情不在工作状态!
加载索引页时,每次运行以下代码:
$(document).ready(function() {
alert('Welcome to index!');
});该代码当前位于assets/javascript/default.js中的文件中
在我的application.html.erb中,我在标题中添加了
<%= javascript_include_tag :default %>在我的控制器中的index format下,我还添加了
format.js { render 'default.js'}当我在脚本下使用firebug运行页面时,它确实看到
<script type="text/javascript" src="/assets/default.js?body=1">虽然我还不理解body=1部分,但在脚本下它确实显示了代码,所以很明显浏览器可以看到代码,但是由于某种原因,它没有执行它。
在我的gem中,我有jquery-rails(1.0.19),捆绑包安装已经运行。
我已经用firefox,internet explorer和chrome尝试过这个页面,但都没有用。如果我已经错过了这样一个问题,我道歉。
感谢大家抽出宝贵的时间。
发布于 2012-01-24 23:19:00
在Rails3.1中,您应该使用'application',而不是'default'
<%= javascript_include_tag :application %>这将加载app/assets/javascript/application.js文件,该文件实际上只是一个清单,用于加载所有其他javascript文件,包括jQuery
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require_tree .它加载jQuery,然后require_tree .行加载所有其他javascript文件。
在您的代码中,jQuery永远不会加载,因此它不理解$函数。
https://stackoverflow.com/questions/8989191
复制相似问题