Rails Specify Load Order of JavaScript Files

Rails specify load order of javascript files?

You've still got a

//= require_tree .

Higher up, which is loading everything, apparently in alphabetical order. Remove that (obviously making sure that everything is required elsewhere) and you should be fine. You might be able to make that the last line of your application.js but I don't remember the specified behaviour when two statements end up requiring the same file

loading order JavaScript files in asset pipeline

Any files in your vendor tree that you need must be required explicitly in your "application.js". It will pull in all the JavaScript in your "app/assets/" tree using the "require_tree".

You probably need to update your file to look something more like this:

//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require rails.validations
//= require that_file_from_vendor_assets
//= require that_other_file_from_vendor_assets
//= require_tree .

where that_file_from_vendor_assets is the vendor JavaScript that you need to be loaded up before it gets to your "app/assets/javascript" files.

Is it okay to change the loading order of JavaScript files in Rails 4?

That's how it's usually done. You'd have to require application.js.coffee before requiring your other custom javascript files(which are loaded with require_tree). Make sure though that application.js.coffee's plugin dependencies are loaded before the require_self.

For example, if you use jQuery, you'd do it this way in your application.js.coffee

#= require jquery
#= require_self
#= require_tree .

Load order with javascript_include_tag :all

You can do as follows

  • First, load the default JavaScript files.
  • Then load other scripts in the order that you want

    <%= javascript_include_tag :defaults %>
    <%= javascript_include_tag "script_1", "script_2", "script_3" %>

loading scripts in a defined order in rails app. how to?

For Rails 3.0:

javascript_include_tag takes an array of sources, and includes them in the order that you define. You can omit :defaults, or define it in your application.rb file with config.action_view.javascript_expansions[:defaults] = %w(foo.js bar.js). :all includes all the javascripts in your /javascripts/ folder.

Therefore, it's best to have jQuery at the beginning, since most or all of your javascript files will use jQuery.

More documentation: http://apidock.com/rails/ActionView/Helpers/AssetTagHelper/JavascriptTagHelpers/javascript_include_tag

Javascript order on Ruby on Rails app

It looks like you're having issues with the order of your includes.
You need to include jQuery before anything that depends on it, and not load it again after (or the changes any plugins made will typically disappear).

If //= require jquery loads jQuery (which is probably the case, else all your plugins would be throwing errors about an undefined jQuery or $), then you should probably get rid of the line that says //= require jquery-1.10.2.min.js. Actually, you should probably get rid of the file as well (unless jquery.js is just a symlink to it), lest //= require_tree . include it anyway.

Understanding require_tree load order

The reason for this is require_tree loads the files or folders alphabetically whether it is a file or folder. One a way of solving your problem is individually loading the file without using require_tree. Another way you can achieve your goal is by adding _ underscore before the file you want to load first. Because require_tree will load the file prefixed with underscore before the other files. So in your case your directory structure would like following

Updated dir structure

How to get javascript files working on rails 5 application

Note my origin index.html file. I had to emulate that behavior in rails.

This is a working solution, all though I am not sure if it is the most efficient or Rails appropiate soultion.

step 1:

I created a javascript file called _js-file.html.erb and included it to the bottom of my application.html.erb file <%= render 'layouts/js-file' %>

<%= javascript_include_tag('jquery.min.js') %>
<%= javascript_include_tag('jquery.dropotron.min.js') %>
<%= javascript_include_tag('jquery.scrollgress.min.js') %>
<%= javascript_include_tag('skel.min.js') %>
<%= javascript_include_tag('util.js') %>
<%= javascript_include_tag('main.js') %>

step 2:

next you have to comply with the precompile assets
go into your assets.rb file found in config/initializers/ folder and add your javascript files.

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



Related Topics



Leave a reply



Submit