Search code examples
ruby-on-rails-3.1asset-pipelinesprockets

requiring asset files from subdirectories of the asset path


I'm in the process of trying to upgrade my app to Rails 3.1, and I have quite a lot of custom Javascript files that I'm trying to migrate to the new assets pipeline.

My current (that is, pre-3.1) directory structure has something like:

public/
    foo/
        model.js
        controller.js
        etc...

So, I'm trying to move these to lib/assets/javascripts and keep the foo/ subdirectory intact (rather than make foo_model.js, foo_controller.js, etc).

In my application.js I tried to do:

//= require foo/model

and

//= require 'foo/model'

But these don't work. Rails always gives me an error page and says:

couldn't find file 'foo/model.js'

I did test that things get correctly included if they're not in the subdirectory, so I know it's not just that my lib/assets directory is not in the include path or something. So I guess I'm just wondering if there's a way to do this, or should I just be flattening the directories and using foo_model.js and such?


Solution

  • Looks like rails only add one level of subdirectories to assets paths

    You can check with current paths by running Rails.application.config.assets.paths at rails console

    add absolute path of your folders at application.rb like

    config.assets.paths += ["/path/to/lib/assets/javascript/foo"]