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

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

问题描述

为了使控制器特定的 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/环境/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 只在 assets/javascripts 和 assets/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

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

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