Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asset pre compilation for subdirectory manifest file

I am using Rails 3.1 and under assets I have files like this:

assets
  javascripts
    admin
        admin.js
        a1.js
    client
       client.js
        c1.js

admin.js looks like this

//
//= require jquery
//= require jquery_ujs
//= require a1

client.js looks like this

//
//= require jquery
//= require c1

Everything works fine in development mode. When I do rake assets:precompile then I do not see any javascript files in public/assets. I do see all the stylesheets in public/assets.

I think this has to do with the fact that manifest files (admin.js and client.js) in this case are in subdirectory.

So is this true that rake assets:precompile does not look into subdirectories?

Any suggestions on how to fix this. I prefer to have the files the way I laid out because I have a bunch of javascript files.

like image 391
Nick Vanderbilt Avatar asked Nov 29 '11 03:11

Nick Vanderbilt


People also ask

How do you Precompile an asset?

To compile your assets locally, run the assets:precompile task locally on your app. Make sure to use the production environment so that the production version of your assets are generated. A public/assets directory will be created. Inside this directory you'll find a manifest.

What does rake assets Precompile do?

RAILS_ENV=production tells Rails to compile the production version of the assets. assets:precompile is a Rails provided rake task that has instructions for compiling the assets.

What are rails Sprockets?

Sprockets is a crucial Ruby library that is used to serve and compile web assets. However, in Rails 7.0 the library becomes an optional dependency unless the application needs to use Sprockets. In such situations, the sprockets-rails gem will have to be added to the Gemfile .


2 Answers

There is a precompile array in the Rails config that sets what files to precompile. application.js and application.css in any directory.

You will need to add your files to the precompile array:

config.assets.precompile += ['admin/admin.js', 'client/client.js']

And they should be accessible via:

javascript_include_tag "admin/admin.js"

and

javascript_include_tag "client/client.js"
like image 171
Richard Hulse Avatar answered Oct 20 '22 15:10

Richard Hulse


At rails 3.2.6, when managing javascript assets in subdirs, you can name the manifest for each subdir 'index.js' (as opposed to OP's 'admin.js' and 'client.js'), and then in config/environments/production.rb say:

config.assets.precompile += ['admin.js', 'client.js']

Magic behind the scenes will look in the admin subdir and compile according to the specs in index.js, outputting to admin.js; likewise for client.

The assets will then be accessible via:

javascript_include_tag 'admin'
like image 21
KenB Avatar answered Oct 20 '22 14:10

KenB