Rails 3.1 预编译控制器特定 JS 资源的策略
为了将控制器特定的 JavaScript 逻辑排除在标准 application.js
之外,并且仅将其包含在相关控制器中,我将其放入自己的 .js 文件中,并根据控制器包含它布局中的名称如下:
<%= javascript_include_tag "application", params[:controller] %>
这工作得很好,但是当我将应用程序部署到生产环境时(我使用 Capistrano 并设置了预编译任务),资产管道不会预编译任何控制器特定的 JS文件。我认为这是因为 application.js 中的 require 指令没有引用我的实际 JavaScript 文件。
如何处理这个问题而不将控制器特定的 JS 移回 application.js,或者从 application.js 显式引用它?
有没有办法告诉资产管道预编译额外的列表文件?如何在生产环境中手动预编译特定文件?
更新
结果,您可以在此处指定单个文件config/environments/Production.rb:
config.assets.precompile += %w( achievements.js )
...或者我只是随意地为每个 JavaScript 文件添加它:
config.assets.precompile += %w( *.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] %>
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.
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?
Update
As it turns out, you can specify individual files here in your config/environments/production.rb
:
config.assets.precompile += %w( achievements.js )
...or I just went ahead and capriciously added it for every JavaScript file:
config.assets.precompile += %w( *.js )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您想预编译 js|css 仅在 asset/javascripts 和 asset/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 :
我认为你和 james_schorr 并不是真正在谈论同一件事。
您需要将application.js以外的文件添加到config.assets.precompile中。如果我没记错的话,他的回答更多的是关于您可以/应该采用的目录结构。
如果我想要特定于控制器,我会这样做:
例如,
users.js
将是:这样,您可以保持组织性(每个控制器有很多 js 文件),但在产品中,它们将全部包含在一个文件中。
您的配置中仍然需要:
I think you and james_schorr are not really talking about the same thing.
You need to add the files other than application.js to config.assets.precompile. His answer was more about the directory structure you could/should adopt, if I'm not mistaken.
If I wanted to have controller specific, I would do:
And for instance,
users.js
would be:That way, you can stay organized (have a lot of js files per controller), but in prod, they will all be included in one file.
Still need that in your config:
这就是我所做的:
目录结构:
application-sessions.js 只是有:
然后在您看来,
在我的 application_controller.rb 方法中执行 FYI,@current_controller =controller_name,使用 before_filter 调用。
This is what I do:
directory structure:
application-sessions.js just has:
Then in your view, do
FYI, @current_controller = controller_name in my application_controller.rb methods, called with a before_filter.
我这里也遇到同样的问题,这让我很头疼。
包含 *.js 有点太多了,我不希望每个文件都编译成单独的文件,其中一些应该合并在一起。
我的想法是,将控制器特定文件存储到子目录中,例如“controllers”,然后仅包含controllers/*.js或css文件进行预编译。
不确定它是否有效,我会尝试一下,无论如何,这可能是一个有用的提示。
I am having same issue here, it's an headache to me.
Including *.js is a little too much, I don't want every files to be compiled into separated files, some of them suppose to be merged together.
My idea is, store the controller specific files into a sub directory, such as "controllers", and then only include controllers/*.js or css files for precompile.
Not sure if it work or not, I will try it out, anyway, it might be a useful hint.