用于预编译控制器特定JS资产的Rails 3.1策略 [英] Rails 3.1 strategy for pre-compiling controller specific JS assets

查看:90
本文介绍了用于预编译控制器特定JS资产的Rails 3.1策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了使特定于控制器的JavaScript逻辑不符合标准application.js,并且仅将其包含在相关的控制器中,我将其放在自己的.js文件中,并根据布局中的控制器名称将其包括在内像这样:

In order to keep controller specific JavaScript logic out of the standard application.js and only have it included by the relevant controller, I'm putting it in its own .js file and including it based on the controller name from the layout like such:

<%= javascript_include_tag "application", params[:controller] %>

那很好,但是当我将应用程序部署到生产环境中时(我正在使用Capistrano并设置了预编译任务),资产管道不会预编译任何特定于控制器的JS文件.我认为这是因为application.js中的require指令未引用我的实际JavaScript文件.

That works just fine, but when I deploy the app to production (I'm using Capistrano and have a pre-compile task set up), the asset pipeline doesn't precompile any of the controller specific JS files. I presume this is because my actual JavaScript file isn't referenced by require directives in application.js.

在不将控制器特定的JS移回application.js或从application.js显式引用它的情况下,如何处理呢?

How do I deal with this without moving my controller specific JS back to application.js, or explicitly referencing it from application.js?

是否有某种方法可以告诉资产管道预先编译其他列表文件?如何在生产中手动预编译特定文件?

Is there some way to tell the asset pipeline to pre-compile an additional list files? How could I manually pre-compile a specific file on production?

更新

结果,您可以在config/environments/production.rb中指定单个文件:

As it turns out, you can specify individual files here in your config/environments/production.rb:

config.assets.precompile += %w( achievements.js )

...或者我只是继续为每个JavaScript文件反复添加了它:

...or I just went ahead and capriciously added it for every JavaScript file:

config.assets.precompile += %w( *.js )

推荐答案

如果要预编译js | css ,仅在asset/javascripts和assess/stylesheets目录的根目录中找到(而不是他们的树的层次结构),您可以将其放在环境文件中:

If you want to precompile the js|css only found in the root of assets/javascripts and assets/stylesheets directories (and not their tree hierarchy), you can put this in environment files :

  Dir.chdir "#{Rails.root}/app/assets/javascripts"
  a = Dir.glob("*.{js,coffee,erb}")
  Dir.chdir "#{Rails.root}/app/assets/stylesheets"
  b = Dir.glob("*.{css,erb}")
  config.assets.precompile +=  a.concat(b)
  Dir.chdir Rails.root

这篇关于用于预编译控制器特定JS资产的Rails 3.1策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆