JavaScript library loaded twice in Rails

When I add the JavaScript library to the Rails resources folder and configure it in the erb file, using: <%= javascript_include_tag('scripts.js') %> , HTML code with classes related to loading the JavaScript file twice.

Here I gave an example here . When I link JavaScript files to a shared folder, this problem does not occur, but JavaScript does not appear instead.

Javascript

 $(function() { $("a.page-scroll").bind("click", function(a) { var b = $(this); $("html, body").stop().animate({ scrollTop: $(b.attr("href")).offset().top }, 1500, "easeInOutExpo"), a.preventDefault() }) }) 

Html.erb

 <li> <a class="page-scroll" href="#portfolio">Portfolio</a> </li> <li> <a class="page-scroll" href="#me">About Me</a> </li> <li> <a class="page-scroll" href="#contact">Contact</a> </li> 

There are no corresponding console errors.

Note. This works fine without Rails.

0
source share
1 answer

If its standard rail comes out of the box and the javascript / coffee file lives inside lib / assets / javascripts or vendor / assets / javascripts, then it will automatically be pulled into the resource pipeline through this line in the application. js file:

// = require_tree.

There is no need to use javascript_include_tag inside the erb file if the application application.js pulled it out (I assume that application.js is rendered by some layout file through javascript_include_tag ('application'), so rendering it twice).

If the js file lives in one of these directories and you delete your javascript_include_tag from the erb file .... It should solve your double-display problem.

+1
source

Source: https://habr.com/ru/post/1238803/


All Articles