Rails 3 asset pipeline: How to prevent some asset files from being loaded for everyone (eg. browser-specific)
-
24-05-2021 - |
Pergunta
I want to server the 'excanvas.min.js' file only to the Internet Explorer and not to other browsers.
So I did in my application.html.erb
:
<!--[if lt IE 9]>
<%= javascript_include_tag 'excanvas.min'
# Excanvas for use of Canvas in IE up to version 8
%>
<![endif]-->
<%= javascript_include_tag 'application' %>
But I get the following error (in production):
Completed 500 Internal Server Error in 3ms
ActionView::Template::Error (excanvas.min.js isn't precompiled)
The error also happens when forcing Rails to precompile the file with
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false
config.assets.precompile += %W(excanvas.min.js,other_file.css,even_anotherfile.js)
in 'config/production.rb'. What am I doing wrong?
I could do a
//= require excanvas.min
in the application.js
. But that would include the file for every browser...
Info: I made an update from an Rails 3.0-Application.
Solução
The problem is in the following line
config.assets.precompile += %W(excanvas.min.js,other_file.css,even_anotherfile.js)
the %W
is a shortcut for creating an array, and the items need to be separated by a space. In your case you are adding the string excanvas.min.js,other_file.css,even_anotherfile.js
to the precompile list.
So just replace the line as follows:
config.assets.precompile += %W(excanvas.min.js other_file.css even_anotherfile.js)
Outras dicas
just try to add <%# %> for line and . i think they will be displayed as comment.
OR
you can do that will js.judge the browser with js and load the corresponding css file