对于JQuery和Rails都是新手,任何帮助都将不胜感激。rails3:未定义jQuery
我正在关注dynamic select menus上的RailsCast教程,但是当我尝试加载表单时,动态选择不起作用,并且在firebug中出现错误,指出JQuery未加载。我检查了assets/application.js,并且该函数似乎正确加载。在我的布局文件中包含javascript_include_tag之前,其他任何内容都会生成。我尝试过预编译我的资产,但似乎没有做任何事情。
奇怪的是我有另一个控制器定义的另一个JQuery函数,该函数工作得很好。我完全被这里发生的事情困惑。
更新 - 添加代码片段。
的application.js
//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require_tree .
application.html.erb
<head>
<title> <%= default_title(yield(:title)) %> </title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
...
<div class="container">
...
<%= yield %>
<%= debug(params) if Rails.env.development? %>
</div>
hostprofiles.js.coffee
JQuery ->
cities = $('#hostprofile_city_id').html()
console.log(cities)
$('#hostprofile_country_id').change ->
country = $('#hostprofile_country_id :selected').text()
escaped_country = country.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\\$1')
options = $(cities).filter("optgroup[lable=#{escaped_country}]").html()
console.log(options)
if options
$('#hostprofile_city_id').html(options)
$('#hostprofile_city_id').parent().show()
else
$('#hostprofile_city_id').empty()
$('#hostprofile_city_id').parent().hide()
产生jQuery函数是的application.js
(function() {
JQuery(function() {
var cities;
cities = $('#hostprofile_city_id').html();
console.log(cities);
return $('#hostprofile_country_id').change(function() {
var country, escaped_country, options;
country = $('#hostprofile_country_id :selected').text();
escaped_country = country.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\\$1');
options = $(cities).filter("optgroup[lable=" + escaped_country + "]").html();
console.log(options);
if (options) {
$('#hostprofile_city_id').html(options);
return $('#hostprofile_city_id').parent().show();
} else {
$('#hostprofile_city_id').empty();
return $('#hostprofile_city_id').parent().hide();
}
});
});
}).call(this);
我们需要看你的代码来帮助你,否则我们会猜测。 – Th0rndike