简体   繁体   中英

Rails javascript files (other than application.js) not working

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();
  });
});

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

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