3

I'm working on implementing jQuery into a rails project, and have this Javascript tag in the head of my application.html.erb file:

  <%= javascript_include_tag "application" %>

And if I put jQuery code into my application.js file, it works as expected in my application. However, if I put the jQuery code into any other .js files in my assets/javascripts folder (home.js, for example), it doesn't work as it should in my application.

Here's my application.js file:

//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require_tree .

It was my impression that the last line of that meant that all my other js files would be compiled into application.js, so having just the <%= javascript_include_tag "application" %> in my application.html.erb file would be sufficient to make all my js files work -- is this a faulty assumption?

These are the contents of my home.js file -- this function works as it should when I put it into my application.js file directly but when it's in my home.js file instead it doesn't work.

$(document).ready(function(){
  $("h2").click(function(){
    $(this).hide();
  });
});
12
  • you are right. where is home.js placed? it should be in app/assets/javascripts/ Commented Feb 21, 2013 at 4:06
  • Yep, that's where it is. It was created when I generated the associated controller and haven't moved it since, so I'm confused by the behavior here. Commented Feb 21, 2013 at 4:18
  • can you check your html source? see if it is in the head tag? Commented Feb 21, 2013 at 4:23
  • Yep -- weird, it's showing up, along with all my other js files (including application.js): <script src="/assets/home.js?body=1" type="text/javascript"></script> Commented Feb 21, 2013 at 4:38
  • 2
    Found it by going to the browser console log -- stupid mistake on my part, had CoffeeScript on at one point so there were lines at the top of another JS file with lines starting with # that were supposed to be comments rather than // after I made them just JS files. Thanks for leading me to the answer! Commented Feb 21, 2013 at 6:18

1 Answer 1

0

Try to add //=my home in application.js file above //=require_tree . Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.