0

I have a vendor library that has its own directories and many files (js and css a.o.). I have placed this as a folder in my_app/vendor/assets/external_library/. How can I refer to files from this directory, for example in my views?

For example, there are the following two files:

  • my_app/vendor/assets/external_library/editor.js
  • my_app/vendor/assets/external_library/contents.css

To use these I have added:

  • In application.js: //= require editor
  • In application.css.scss: @import "contents";
  • In my view: <script src="editor.js"></script>

It does load the view page in development. But when I go to the page's source code and click on the link to the js file, it is unable to find that file. Am I doing something wrong?

Nick
  • 3,496
  • 7
  • 42
  • 96

1 Answers1

2

Add this to your application.rb file:

config.assets.paths << Rails.root.join('vendor', 'assets')

and then require these in your application.js:

//= require external_library/editor.js

and application.css.scss:

@import "external_library/contents";

In your view you should only need to have the following:

= stylesheet_link_tag    "application", media: "all"
= javascript_include_tag "application"
Lewis Buckley
  • 1,583
  • 15
  • 22
  • Thanks! What if I only want the files included in 1 specific view (since it's only relevant for one specific view, so why load it for all views...)? I also found this post: http://stackoverflow.com/a/25985318. One of the answers also suggests editing `config.assets.paths` but a different answer got a lot more votes. Which would be the better solution? – Nick Sep 12 '15 at 15:29
  • No problem. Usually I wouldn't worry about having a different CSS file for each view. Use good class names (you can use the `flutie` gem to automatically give each page a different class) and Rails will just generate one large application.css file for you. Because of the way HTTP works this large css file can be downloaded once and then cached and reused on every page so it actually works out quicker. See this SO answer: http://stackoverflow.com/questions/2336302/single-huge-css-file-vs-multiple-smaller-specific-css-files – Lewis Buckley Sep 12 '15 at 15:36
  • Thanks again Lewis. I was particularly referring to the js file. This file is only needed in one specific view. I was also wondering about the line that adds to assets.paths. Is this really necessary? Particularly, since the answer in the post I referred to doesn't seem to need to make this change to the assets path.\ – Nick Sep 12 '15 at 15:41
  • If you really want to I suppose you could bypass the asset pipeline and just put the JS file in your `public` directory but I really wouldn't recommend it. and then use `` in your view. – Lewis Buckley Sep 12 '15 at 15:46