0

I am using different javascript libraries in different part of my rails app. Currently all my library files are in application.js manifest file. And it gets loaded in pages, where it's not required. Is there any way I can load files that are needed in a particular part of my app?

3
  • You probably want to create a separate layout file. Commented Jul 11, 2016 at 16:18
  • Is that the only way? Commented Jul 11, 2016 at 16:19
  • I read require js can be used. Will it solve my purpose? Commented Jul 11, 2016 at 16:20

1 Answer 1

2

You can add a separate "manifest" and load that on demand where you need it. You will have to:

1- Tell Rails to preprocess those files, ie if your file is named additional_dependencies.js, then add it in assets.rb:

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

2- Load that file where you need it (NOT in the main layout):

= javascript_include_tag 'additional_dependencies'

And, of course, do not add the files you want to keep as separate dependencies into the application.js file (watch for require_tree).

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.