0

I put some js code in the application.js fileand it does not work...

application.js:

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

//...

obj.dd.on('click', function(event){
    $(this).toggleClass('active');
    return false;
});

//...

$(function() {

    var dd = new DropDown( $('#dd') );

    $(document).click(function() {
        // all dropdowns
        $('.wrapper-dropdown-5').removeClass('active');
    });

});

also iI activated a line in the initializers/assets.rb file:

 Rails.application.config.assets.precompile += %w( search.js )

but anyway my code is unavailable for using in a view. I think there should be nothing extra, as it is in the application.js.

Or may be there should be something else?

1
  • On what environment you have this problem? Commented Apr 14, 2015 at 7:26

3 Answers 3

1

1) Have you mentioned this in your application layout??

<%= javascript_include_tag 'application', 'data-turbolinks-track' =>  true %>

2) After this, use alert message in your application.js

alert('Hello');
Sign up to request clarification or add additional context in comments.

Comments

0

Working javascript that is in applications.js will work. So I'm guessing your problem isn't the application.js part of it, it's your function isn't working.

Add a console.log 'clicked' to your click function to see if fires on click. I think your javascript problem could be tied to your click function not in a document.ready() function.

Comments

0

Rails.application.config.assets.precompile += %w( search.js )

By looking above line of code. I guess you have search.js file. Whenever you want to use external js file(manually added) then you have to inform application.js file to include such file.

So your application.js file should be( If you wanted to add search.js which is not under app/assets/javascript/ and placed under vendor/assets/javascript/)

//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require search  #js file will be added like this without it's extension 
//= require_tree .

Note: //= require_tree will include all js files which are under app/assets/javascript/ folder.

3 Comments

If you are using //= require_tree then there is no need to mention //= require search explicitly.
@Umar : But If search.js file is under vendor folder then I think it's required
Yeah, but then don't use //= require_tree

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.