Andrew
Andrew

Reputation: 43153

Rails Asset Pipeline: Precompile Assets outside of manifest?

I have certain page-specific js and css files that I either:

  1. Don't want running on development (ie tracking code)
  2. Don't want running on every page

or both.

In Rails 3.0.x I would simply call them from the view where they are needed like so:

- if some_condition
  = javascript_include_tag 'page_specific'

Now, using the asset pipeline, it looks like I must include these in the manifest (and therefore in every page for the application) or else they won't be precompiled. As I'm using Heroku for deployment allowing lazy compilation is not an option.

How can I manually precompile every file from the assets directory without having to include them all in the manifest? Is this possible?

Upvotes: 3

Views: 708

Answers (1)

royvandermeij
royvandermeij

Reputation: 111

Just don't put the page specific asset in the manifest and include them when you need it like you did in rails 3.0.x, the precompiler will compile those page specific as separate files.

Personally I just check for a certain element I know is in the dom in that page. If the element isn't found the rest of the code isn't executed.

$(function(){
   if( $("#page_specific_element").length !== 1 ) return;
   //page specific functions
});

Upvotes: 3

Related Questions