简体   繁体   English

Rails 3资产管道:如何防止每个人都加载某些资产文件(例如,特定于浏览器)

[英]Rails 3 asset pipeline: How to prevent some asset files from being loaded for everyone (eg. browser-specific)

I want to server the 'excanvas.min.js' file only to the Internet Explorer and not to other browsers . 我想将“ excanvas.min.js”文件仅服务器提供给Internet Explorer,而不是其他浏览器

So I did in my application.html.erb : 所以我在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 强制Rails使用以下命令预编译文件时,也会发生此错误:

# 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'. 在“ config / production.rb”中。 What am I doing wrong? 我究竟做错了什么?

I could do a 我可以做一个

//= require excanvas.min

in the application.js . application.js But that would include the file for every browser... 但这将包括每个浏览器的文件...

Info: I made an update from an Rails 3.0-Application. 信息:我从Rails 3.0应用程序进行了更新。

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. %W是创建数组的快捷方式,项目之间需要用空格分隔。 In your case you are adding the string excanvas.min.js,other_file.css,even_anotherfile.js to the precompile list. 在您的情况下,您excanvas.min.js,other_file.css,even_anotherfile.js字符串excanvas.min.js,other_file.css,even_anotherfile.js到预编译列表中。

So just replace the line as follows: 因此,只需按如下所示替换行:

config.assets.precompile += %W(excanvas.min.js other_file.css even_anotherfile.js)

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 您可以这样做js。使用js判断浏览器并加载相应的CSS文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM