Rails 3.1 - 如何判断资产是否在生产环境中进行预编译?

发布于 2024-12-03 00:05:19 字数 748 浏览 0 评论 0原文

试图掌握部署 Rails 3.1 应用程序的窍门...

根据我所读到的内容,我已将以下代码放入我的deploy.rb中:

before "deploy:symlink", "assets:precompile"

namespace :assets do
  desc "Compile assets"
  task :precompile, :roles => :app do
    run "cd #{release_path} && rake RAILS_ENV=#{rails_env} assets:precompile"
  end
end

但说实话,我看不出与或没有它。我在这里缺少什么吗?

编辑*找到了答案:

http://spreecommerce.com/blog

要预编译生产资源,您通常会执行以下 rake 任务(在生产服务器上)。

$bundle exec rake 资产:预编译 这会将所有资产写入 public/assets 目录,同时在文件名中包含 MD5 指纹以增加缓存优势。

注意:在生产中,使用 image_tag、asset_path、javascript_include_tag 等对视图中资产的所有引用都会自动在文件名中包含此指纹,以便提供正确的版本。

Trying to get the hang of deploying a rails 3.1 App ...

Based on what I've read, I've put the following code in my deploy.rb:

before "deploy:symlink", "assets:precompile"

namespace :assets do
  desc "Compile assets"
  task :precompile, :roles => :app do
    run "cd #{release_path} && rake RAILS_ENV=#{rails_env} assets:precompile"
  end
end

But to tell you the truth, I can't notice any difference with or without it. Is there something I'm missing here?

EDIT* found the answer:

http://spreecommerce.com/blog

To pre-compile assets for production you would normally execute the following rake task (on the production server).

$ bundle exec rake assets:precompile
This would write all the assets to the public/assets directory while including an MD5 fingerprint in the filename for added caching benefits.

NOTE: In production all references to assets from views using image_tag, asset_path, javascript_include_tag, etc. will automatically include this fingerprint in the file name so the correct version will be served.

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

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

发布评论

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

评论(1

天煞孤星 2024-12-10 00:05:19

需要进行配置,但默认情况下应该正确设置。进入您的 config/application.rb 并查看是否找到以下内容:

if defined?(Bundler)
  # If you precompile assets before deploying to production, use this line
  Bundler.require(*Rails.groups(:assets => %w(development test)))
  # If you want your assets lazily compiled in production, use this line
  # Bundler.require(:default, :assets, Rails.env)
end
...
config.assets.enabled = true

您的 production.rb 文件中也应该有这些内容:

# Compress JavaScripts and CSS
config.assets.compress = true

# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false

应该这样设置。是吗?

There is configuration to do, but it should be correctly set by default. Get in your config/application.rb and see if you find this:

if defined?(Bundler)
  # If you precompile assets before deploying to production, use this line
  Bundler.require(*Rails.groups(:assets => %w(development test)))
  # If you want your assets lazily compiled in production, use this line
  # Bundler.require(:default, :assets, Rails.env)
end
...
config.assets.enabled = true

You should also have those in your production.rb file:

# Compress JavaScripts and CSS
config.assets.compress = true

# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false

This should be set that way. Is it?

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