Michael Stark
Michael Stark

Reputation: 645

JavaScript won't work on my rails app

I have a problem with running js/jquery in my rails app. I have Ruby 1.9.3 and Rails 3.2.3 installed.

I have two js files in /app/assets/javascripts:

application.js
main.js

I created this main.js. In it there is this code

$(document).ready(function() {
    alert('Hello world!');
});

my application.js has the following:

//= require jquery
//= require jquery_ujs
//= require_tree .
//= require main.js

When I open my app, no alert message pops up!

Opening the source code, i see all the js files in place:

<script src="/assets/jquery.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery_ujs.js?body=1" type="text/javascript"></script>
<script src="/assets/main.js?body=1" type="text/javascript"></script>
<script src="/assets/application.js?body=1" type="text/javascript"></script>

What I am doing wrong???

Upvotes: 1

Views: 1791

Answers (4)

Michael Stark
Michael Stark

Reputation: 645

Ok guys, I made a mistake. I gave false information about the name of my js file for abstracting purposes. The originial name of my was advert.js and for whatever reason it does not get executed as js. I remembered that I used advert.css in this project and it was also not recognized by rails as css, even if it was loaded. I renamed that advert.css to popup.css and it worked, so it did now with my advert.js. Renamed it to popup.js and it works!

Upvotes: 0

kasper375
kasper375

Reputation: 171

Try to write alert($); or console.log($); on the top of main.js.

Is $ undefined?

Upvotes: 0

Mesh
Mesh

Reputation: 6422

jQuery.js and jquery_ujs.js are not in your assets folder according to your post

Upvotes: 0

TheIrishGuy
TheIrishGuy

Reputation: 2573

//= require_tree .

should be the last line always in your application.js file

$(document).ready(function() {
    alert('Hello world!');
})

doesn't need the last semicolon, also

Upvotes: 1

Related Questions