0

Noted Things

Rails 4.1.8

jquery rails 3.1.3

Windows 7

I am currently trying to run a simple coffescript under app/assets/javascripts/login.jscoffe

#   login.js.coffee   #
$(document).on "page:change", ->
  $('#loginb').click ->
      alert "Clicked!"

When I load up the page I get a console error.

Uncaught reference error $ is not defined

Now I have tried several things, such as using rake precompile. Made sure I included in application js below.

//= require jquery
//= require jquery_ujs

I've been reading up to see if there are other solutions. Some saying things about turbolinks. Should I just include the jquery right into the html page?

MooCow
  • 1,397
  • 1
  • 12
  • 30
  • included jquery? No I have not . – MooCow Jul 20 '15 at 15:40
  • The require statements are already added above – MooCow Jul 20 '15 at 15:45
  • I could suggest to investigate the problem. First, check in your source page whether jQuery file loaded or not by doing `Ctrl+u` or `view pagesource`. And if it was loaded successfully, try using `jQuery` instead of `$` symbol. If it works we think some other JS library conflicts with the jQuery in your application. – Venkat Ch Jul 20 '15 at 15:59
  • In the head tag there is only a script tag linking to the src of login.js. Rails should automatically include the jquery src too? The coffee scripts seems to to throw exceptions when indentation rules are broken. So the coffee script seems to be functional.... – MooCow Jul 20 '15 at 16:13
  • As you are saying that there is only one src in the source then is your `application.js` included in the layout? If yes then it should have jquery in source too. – Deepesh Jul 20 '15 at 17:38
  • An important step I see overlooked a lot is script load order. You need to be sure your jQuery lib scripts are loaded before your script that calls jQuery functionality is loaded. – Bacon Brad Jul 20 '15 at 17:55

1 Answers1

0

So I back upped a little I how I set up my first app. Using a windows 7 machine you run into a problem defined in this article ---> ExecJS::RuntimeError on Windows trying to follow rubytutorial

Since I took the easy way out(going into application.html.erb and changing the linking tag from "application" to "default" fixed a problem but also created more. I then had to load each script I wanted using the include tag and precompile assets files.

The solution came in when I took a step back and used the answer to the link above. Fixing the ExecJs and reintegrating the default functionally of the app seem to fix things.

Community
  • 1
  • 1
MooCow
  • 1,397
  • 1
  • 12
  • 30