强制rails创建资源缓存文件
我在 s3 上托管我的资产。 在生产中,rails 正在寻找 /javascripts/cache/all.js 和 /stylesheets/cache/all.css。 当我使用 cap 进行部署时,我正在使用一个插件将公共目录转移到 s3。 问题是,在第一次请求这些缓存文件之前,rails 不会创建这些文件,因此当我传输公共目录时,它们在部署期间并不存在。 有没有一种简单的方法可以在部署期间强制创建这些文件?
I am hosting my assets on s3. In production, rails is looking for /javascripts/cache/all.js and /stylesheets/cache/all.css. I'm using a plugin to swoop the public directory over to s3 when I deploy with cap. The problem is that rails doesn't create these cache files until they are requested for the first time, so they aren't around during deployment when I transfer the public dir. Is there an easy way to force the creation of these files during deployment?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我在 rake 任务文件中使用它。 使您无需创建额外的类,并且可以保持干燥,因为您使用的是
stylesheet_link_tag / javascript_include_tag
中的值I use this in a rake task file. Saves you from creating an additional class and you keep it DRY because you're using the values from the
stylesheet_link_tag / javascript_include_tag
如果您使用的是 Engine Yard Cloud,这就是我所做的:
添加文件 /lib/tasks/assetcache.rake
然后添加 /lib/assetwriter.rb
require 'action_view'
类 AssetCacheWriter
包括 ActionView::Helpers::AssetTagHelper
def write
write_asset_file_contents(File.join(JAVASCRIPTS_DIR, "cache/all.js"),compute_javascript_paths([:all], true))
write_asset_file_contents(File.join(STYLESHEETS_DIR, "cache/all.css"),compute_stylesheet_paths([:all], true))
end
end
然后我添加到 /deploy/after_restart.rb
run "cd #{release_path} && bundle exec rake assetcache:generate"
If you're using Engine Yard Cloud, this is what I did:
added a file /lib/tasks/assetcache.rake
then I added /lib/assetwriter.rb
require 'action_view'
class AssetCacheWriter
include ActionView::Helpers::AssetTagHelper
def write
write_asset_file_contents(File.join(JAVASCRIPTS_DIR, "cache/all.js"), compute_javascript_paths([:all], true))
write_asset_file_contents(File.join(STYLESHEETS_DIR, "cache/all.css"), compute_stylesheet_paths([:all], true))
end
end
then I added to /deploy/after_restart.rb
run "cd #{release_path} && bundle exec rake assetcache:generate"
我发现包含顺序很重要(例如,jQuery 在其他脚本之前包含),因此使用了以下方法。 还允许使用 :defaults。
首先lib/asset_pacakger:
然后是scripts/package_assets:
然后是cap任务:
I found that include order mattered (so, for example, jQuery is included before other scripts) and so used the below approach instead. Also allows for the use of :defaults.
First lib/asset_pacakger:
Then scripts/package_assets:
Then a cap task:
在 Rails 3.0.7 中,上述方法似乎都不起作用。 我收到很多关于配置未定义的错误。
在亲自查看 Rails 代码后,我想出了这个对我有用的解决方案。
lib/tasks/cache_assets.rake
然后是 lib/asset_packager.rb
然后要执行它,只需运行
rake cache_assets
In Rails 3.0.7 none of the above approaches seemed to work. I got many errors about config being undefined.
After getting my hands dirty looking over the Rails code, I came up with this solution that is working for me.
lib/tasks/cache_assets.rake
And then lib/asset_packager.rb
Then to execute it, just run
rake cache_assets
创建缓存文件的实际 Rails 模块是
ActionView::Helpers::AssetTagHelper
,您可以在部署期间重用它来创建缓存文件。 以下对我来说效果很好:这两个
write_asset_file_contents
调用都是我从 Rails 代码中提取的。_DIR
常量在包含的模块中定义,compute_xxx_paths
调用中的true
参数指示应递归包含所有文件。我创建了一个简单的 cap 任务,只写出文件:
The actual rails module that creates the cache files is
ActionView::Helpers::AssetTagHelper
which you may be able to re-use to create the cache files during deployment. The following worked okay for me:Both those
write_asset_file_contents
calls I've lifted from the rails code. The_DIR
constants are defined in the included module and thetrue
parameters in thecompute_xxx_paths
calls indicate that all files should be included recursively.I created a simple cap task that just wrote the files out: