Skip to content

Instantly share code, notes, and snippets.

@snikch
Created May 3, 2012 02:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save snikch/2582549 to your computer and use it in GitHub Desktop.
Save snikch/2582549 to your computer and use it in GitHub Desktop.
Prevent AssetNotPrecompiledError before it occurs.
# Checks that the precompile list contains this file or raises an error, in dev only
# Note: You will need to move config.assets.precompile to application.rb from production.rb
def javascript_include_tag *sources
sources.each do |source|
raise "Hey, #{source} is not in the precompile list. This will fall apart in production." unless Rails.application.config.assets.precompile.any? do |matcher|
if matcher.is_a? Proc
matcher.call(source)
elsif matcher.is_a? Regexp
matcher.match(source)
else
matcher.gsub(/\.js/,'') == source.gsub(/\.js/,'')
end
end
end if Rails.env.development?
super *sources
end
@snikch
Copy link
Author

snikch commented May 3, 2012

Add this inside a helper (We have app/helpers/javascript_helper).

This will prevent Sprockets::Helpers::RailsHelper::AssetPaths::AssetNotPrecompiledError errors in production by checking that the javascript file you're trying to include is in the list of assets to be precompiled.

Note: You will need to move any additions to config.assets.precompile from config/environments/production.rb to config/application.rb.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment