如何仅为开发环境要求资产

时间:2021-05-21 23:33:00

I'm using the assets pipeline from Rails 3.1 and I want to include some javascript files only if it's the development environment.

我正在使用Rails 3.1中的资产管道,我想只在开发环境中包含一些javascript文件。

Example:

//= require application/jquery
//= require application/jquery_ujs
// next ones only for development environment
//= require application/badglobals
//= require application/consul

Its there a standar way of doing this? Any suggestions?

这有一个标准的做法吗?有什么建议么?

Update

Looking at the Sprockets current documentation, seems like there is not a way to do this.

看看Sprockets当前的文档,似乎没有办法做到这一点。

2 个解决方案

#1


11  

Why not just require these in the view? Is it important that they are loaded in the asset? To load them in the view:

为什么不在视图中要求这些?它们加载到资产中是否重要?要在视图中加载它们:

<% if Rails.env.development? %>
  <%= javascript_include_tag "application/badglobals" %>
  <%= javascript_include_tag "application/consul" %>
<% end %>

#2


0  

If you rename your application.js file (or whichever file you're calling //= require ... in) to application.js.erb, you can take advantage of require_asset. i.e:

如果将application.js文件(或您要调用的任何文件// = require ... in)重命名为application.js.erb,则可以利用require_asset。即:

//= require application/jquery
//= require application/jquery_ujs
<% if Rails.env.development? %>
  <%= require_asset 'application/badglobals' %>
  <%= require_asset 'application/consul' %>
<% end %>

Source: https://github.com/sstephenson/sprockets/issues/90

#1


11  

Why not just require these in the view? Is it important that they are loaded in the asset? To load them in the view:

为什么不在视图中要求这些?它们加载到资产中是否重要?要在视图中加载它们:

<% if Rails.env.development? %>
  <%= javascript_include_tag "application/badglobals" %>
  <%= javascript_include_tag "application/consul" %>
<% end %>

#2


0  

If you rename your application.js file (or whichever file you're calling //= require ... in) to application.js.erb, you can take advantage of require_asset. i.e:

如果将application.js文件(或您要调用的任何文件// = require ... in)重命名为application.js.erb,则可以利用require_asset。即:

//= require application/jquery
//= require application/jquery_ujs
<% if Rails.env.development? %>
  <%= require_asset 'application/badglobals' %>
  <%= require_asset 'application/consul' %>
<% end %>

Source: https://github.com/sstephenson/sprockets/issues/90