简体   繁体   English

New Rails 3.2.8应用程序未加载jQuery

[英]New Rails 3.2.8 app not loading jQuery

I did a rails new today, then created a simple search bar: 我今天做了rails newrails new ,然后创建了一个简单的搜索栏:

<form>
  <input id='search-bar' type='search' name='search'></input>
</form>
<a id='search-btn' href="#">searching!</a>

Alright, how about some click handlers! 好吧,一些点击处理程序呢! So assets/javascripts/searches.js.coffee: 因此,assets / javascripts / searches.js.coffee:

$('#search-btn').click ->
  alert 'BOOM'
  query = $('#search-bar').html
  console.log query

And... nothing. 没事了。 So I go to the Chrome console: 因此,我转到Chrome控制台:

>$('a')
null
>$('body')
null
>$
function () { [native code] }

a jQuery with no selectors, not too helpful. 没有选择器的jQuery,不是太有用。 What is going on here? 这里发生了什么? If I add <script type="text/javascript" src='http://code.jquery.com/jquery-1.8.2.js'></script> to the page or my layout.html.erb, we're all good. 如果将<script type="text/javascript" src='http://code.jquery.com/jquery-1.8.2.js'></script>到页面或我的layout.html.erb,我们都很好。

I'm having the same issue with underscore-min.js (note it is not .min.js) not loading despite being in assets/javascripts. 我遇到了同样的问题,尽管位于资产/ javascript中,但underscore-min.js(注意不是.min.js)无法加载。 Given my (unmodified) application.js, it should load the current folder: 给定我的(未修改的)application.js,它应该加载当前文件夹:

//= require jquery
//= require jquery_ujs
//= require_tree .

UPDATE UPDATE

If I go to the Chrome inspector's Resources tab and check out application.js, I see this: 如果我转到Chrome检查器的“资源”标签并签出application.js,则会看到以下内容:

// 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 .
;

That doesn't look like it is properly including the js files inside its directory 看起来好像没有正确在其目录中包含js文件

Sprockets aren't working for some reason. 链轮由于某种原因无法工作。 Check that you are properly including all the assets-related gems and that you didn't change config/environments/development.rb. 检查您是否正确包含了所有与资产相关的gem,并且您没有更改config / environments / development.rb。 Obviously make sure there isn't application.js in public/javascripts/, it should be in app/assets/javascripts. 显然,请确保在public / javascripts /中没有application.js,应该在app / assets / javascripts中。

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

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