Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 936
bratsche Avatar asked Aug 22 '11 02:08

bratsche


2 Answers

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"]
like image 67
YOU Avatar answered Sep 28 '22 15:09

YOU


Not sure why rails only adds one level of subdirectories, but you can surely add additional (fully qualified) paths to the pipeline in config/application.rb. The docs lead to the answer: http://guides.rubyonrails.org/asset_pipeline.html#asset-organization

For example:

config.assets.paths << "#{Rails.root}/app/assets/flash"

like image 44
Alan David Garcia Avatar answered Sep 28 '22 16:09

Alan David Garcia