jQnoob
jQnoob

Reputation: 165

Load assets in Rails 3.2 with Modernizr.load

I want to be able to load in certain JS files using Modernizr.load --

 Modernizr.load({
  test : Modernizr.touch,
  yep  : '/assets/mobile.js',
  nope : '/assets/desktop.js',
});

but when this get compiled for production, those paths do not exist anymore. How can I sort out loading in this way?

Upvotes: 0

Views: 377

Answers (1)

torybriggs
torybriggs

Reputation: 66

You will need to add those assets to config.assets.precompile in your environment config file (found in config/environments/production.rb). Here's an example:

config.assets.precompile += %w(mobile.js desktop.js)

These assets can then be referenced by Modernizr:

Modernizr.load({
  test : Modernizr.touch,
  yep  : '/assets/mobile.js',
  nope : '/assets/desktop.js',
});

EDIT: You should also make sure these assets are NOT included in your application.js manifest. Including them in application.js would defeat the purpose of conditionally loading them for specific feature-less browsers.

Upvotes: 2

Related Questions