如何仅在开发环境中需要资产

发布于 2024-12-07 22:03:14 字数 375 浏览 0 评论 0原文

我正在使用 Rails 3.1 中的资产管道,并且仅当它是开发环境时我才想包含一些 javascript 文件。

示例:

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

有这样做的标准方法吗?有什么建议吗?

更新

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

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素衣风尘叹 2024-12-14 22:03:15

为什么不在视图中只需要这些呢?将它们加载到资产中重要吗?要将它们加载到视图中:

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

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 %>
¢蛋碎的人ぎ生 2024-12-14 22:03:15

如果您将 application.js 文件(或您在其中调用 //= require ... 的任何文件)重命名为 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 %>

来源:https://github.com/sstephenson/sprockets/issues/90

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:

//= 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文