14

I have organized my javascript files in a couple of directories and I have found the following strange behavior. Given the following tree:

+ app
  + assets
    + javascripts
      + common
      + public
        + common
        + home
          - home.js

home.js looks like this:

//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require_directory ../../jquery_plugins
//= require_directory ../../common
//= require_directory ../common
//= require_self

Now the trick lies in the jquery_plugins directory. I placed this inside vendor/assets/javascripts (which is included in the asset load path, when I check Rails.application.config.assets.paths). When I do this I get the error: require_tree argument must be a directory. When I move that directory to app/assets/javascripts then everything works.

Does anybody have a clue as to what I'm doing wrong? Or is this a bug?

Pascal Lindelauf
  • 4,782
  • 4
  • 40
  • 55

2 Answers2

27

You could add a manifest file to the directory you are trying to serve with something like vendor/assets/javascripts/jquery_plugins/manifest.js

//= require_directory .

and require it in your app/assets/javascripts/application.js via

//= require jquery_plugins/manifest

Edit (even simpler way)

Thanks to @LeEnno for this

You can actually put all your single library related files in a folder named after the library for example vendor/assets/javascripts/bootstrap and in that same folder add an index.js which will act as your manifest and Rails will automatically pick it up

if in your

app/assets/javascripts/application.js

you add the line

//= require bootstrap

SO EASY!!!
Link to Rails Asset Pipeline Guide

mraaroncruz
  • 3,780
  • 2
  • 32
  • 31
  • I am still doing this so if there is a reason for the negative votes, I would love some feedback. – mraaroncruz Mar 14 '12 at 15:45
  • 3
    way cleaner than the up-voted one...what is wrong with people – Plattsy Mar 27 '12 at 11:08
  • This does seem cleaner, I like it. I'm hesitant to do this, though, because I like to have *only* vendor code in vendor/, so that if I'm changing or updating stuff in there I don't have to worry about overwriting code I've written. I suspect similar thinking is where the (former?) downvotes came from. – jpadvo May 29 '12 at 23:57
  • 1
    @jpadvo - this does keep everything in the `vendor/assets` folder. Maybe I don't understand your issue. - this was downvoted three times I think, haven't deleted comment because then @Plattsy's comment wouldn't make any sense :D – mraaroncruz May 31 '12 at 12:52
  • @pferdefleisch Lol. Anyway, the manifest file would custom code for your project (even though it is really simple). I don't like creating any files in vendor/ even simple ones. This is a pretty clean approach, though, and all told might be worth it. – jpadvo May 31 '12 at 19:38
  • 1
    If you rename the *manifest.js* to *index.js* you can simply do `//= require jquery_plugins` in *app/assets/javascripts/application.js*. This is what the [Rails Asset Pipeline Guide](http://guides.rubyonrails.org/asset_pipeline.html#using-index-files) suggests. – LeEnno May 26 '13 at 16:40
  • @LeEnno Finally updating this :) – mraaroncruz Jul 21 '13 at 14:15
12

I had the same problem. I'm still not sure if it's a bug or intentional behavior, but it seems that Rails.application.config.assets.paths only works for single files, i.e. require jquery and so on. Apparently the asset load paths are just used to return the best match for a single require but not for require_directoryor require_tree.

In my case, to load all files from vendor/assets/javascripts, I had to add the following to my app/assets/javascripts/application.js:

//= require_tree ../../../vendor/assets/javascripts/.

In your case something like this should work:

//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require_directory ../../../../../vendor/assets/javascripts/jquery_plugins
//= require_directory ../../common
//= require_directory ../common
//= require_self

It seems that you always have to use the relative path from file where you're calling require_directory or require_tree.

Also, I found this discussion on the structuring of JS-assets to be helpful: Rails 3.1 asset pipeline and manually ordered Javascript requires

Community
  • 1
  • 1
Frane
  • 514
  • 3
  • 11