【问题标题】:Images and assets not working in my production server on rails 3.1.0图像和资产在 Rails 3.1.0 上的生产服务器中不起作用
【发布时间】:2011-09-07 21:18:48
【问题描述】:

我将服务器切换到生产环境,但无法加载任何图像。它在开发模式下一切正常,但是当我切换到生产模式时,它都停止了工作,我启用了 server_static_assets,但仍然没有任何效果。对此有任何帮助

【问题讨论】:

  • 甚至 image_tag 不工作
  • 我可以在我的服务器日志中看到这个cache: [GET /assets/icon_thumb.png] stale, valid, store

标签: ruby-on-rails ruby-on-rails-3 asset-pipeline


【解决方案1】:

以下是您可能遇到的一些问题:

1 - 您的生产配置可能不正确。如果您从早期的 3.1 候选版本开始,并且一直在更新,那么这种情况尤其可能发生。 production.rb 的建议选项在 rc4 和 3.1.0 版本之间发生了很大变化。

确保您的 production.rb 设置包括:

# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = false
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false
# Generate digests for assets URLs
config.assets.digest = true

2 - 您可能忘记预编译您的资产

RAILS_ENV=production rake assets:precompile

3 - 您可能忘记重新启动 Web 服务器以获取 production.rb 中的更改。

【讨论】:

  • 是的,谢谢,我还必须运行 rake rails:update 才能完成所有操作
【解决方案2】:

记得在你的生产环境中运行rake assets:precompile

如果你需要使用 Capistrano 进行部署,你可以使用这个秘籍:

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

【讨论】:

  • 你也可以将load 'deploy/assets'添加到你的deploy.rb
【解决方案3】:

如果您正在升级到 Rails 4 或当前正在生产中使用它,并且正在从 css 加载图像,那么:

而不是

background-image: url('some_image.jpg');

background-image: image-url('some_image.jpg');

参考http://guides.rubyonrails.org/asset_pipeline.html#css-and-sass

【讨论】:

  • 这在使用js库时是不现实的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-22
  • 2016-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多