简体   繁体   中英

New Rails 3.2.8 app not loading jQuery

I did a rails new today, then created a simple search bar:

<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:

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

And... nothing. So I go to the Chrome console:

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

a jQuery with no selectors, not too helpful. 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.

I'm having the same issue with underscore-min.js (note it is not .min.js) not loading despite being in assets/javascripts. Given my (unmodified) application.js, it should load the current folder:

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

UPDATE

If I go to the Chrome inspector's Resources tab and check out application.js, I see this:

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

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. Obviously make sure there isn't application.js in public/javascripts/, it should be in app/assets/javascripts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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