【问题标题】:Rails 3.2.13 - Assets are not displayed in productionRails 3.2.13 - 资产未在生产中显示
【发布时间】:2014-09-29 08:33:17
【问题描述】:

我花了一整天的时间试图弄清楚这个问题,但没有任何积极的结果。 当我加载我的网站时,没有 CSS、图像或工作 javascript。

应用在 Rails 3.2.13 和 Capistrano 2 上运行。

这是我的设置:

config/environments/production.rb

Appname::Application.configure do
  config.cache_classes = true

  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true

  #config.serve_static_assets = false
  config.serve_static_assets = true

  config.assets.compress = true
  config.assets.compile = true
  config.assets.precompile = ['*.js', 'application.css', 'styles.css.scss', '*.css.erb']
  config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif *.svg *.woff *.ttf *.ico)

  config.assets.digest = true
  config.cache_store = :memory_store

  config.i18n.fallbacks = true
  config.active_support.deprecation = :notify
end

application.rb

require File.expand_path('../boot', __FILE__)

require 'rails/all'

if defined?(Bundler)
  Bundler.require(*Rails.groups(:assets => %w(development test)))
end

module Apname
  class Application < Rails::Application
    config.encoding = "utf-8"
    config.filter_parameters += [:password]

    config.active_support.escape_html_entities_in_json = true
    config.active_record.whitelist_attributes = true

    config.assets.enabled = true

    config.assets.paths << "#{Rails.root}/app/assets/fonts"
    config.assets.precompile << /\.(?:svg|eot|woff|ttf)\z/

    config.assets.version = '1.0'
    config.assets.initialize_on_precompile = false
  end
end

我在本地运行bundle exec rake assets:precompile -> 文件生成到public/assets 目录。

但仍然 - 生产中没有资产。

我已经不知道要在配置文件中更改什么,但在生产服务器上仍然是相同的结果。

我们将非常感谢您的每一次帮助!

编辑:

我刚刚在控制台中注意到图像已正确加载(我可以加载 http://website.com/assets/social_icons/facebook_ico.png),但不是 CSS+JS(http://IP/assets/application-3b6ba7703e465259e4e7ebee17c5ea1e.js 404 NOT FOUND - .css 也是如此)

【问题讨论】:

  • 我看到该模块被称为“Appname”,但配置正在寻找“Appname” - 只是您的示例中的一个错字,或者这是从实际代码中复制的?如果您将其称为一件事,然后尝试访问另一件事...问题!
  • Appname 只是一个占位符,代码中是应用的真实名称。
  • 我假设您正在使用“git”来推动生产?您是否允许 git 在您正在推送的分支中记录资产?它们实际上在 repo 中并且没有被忽略?

标签: ruby-on-rails ruby capistrano assets production


【解决方案1】:

您是否在编译资产后尝试重新启动网络服务器?

权限设置是否正确(chmod 755 . -R)?

另外,检查您的网络服务器配置是否指向 app_root/public 而不仅仅是 app_root

编辑1:

检查是否可以直接加载 url。此外,验证呈现的页面是否显示正确缓存的 url。

如果两者之一失败,请尝试强制编译生产环境

rake assets:precompile RAILS_ENV=production

编辑2: 如果没有任何效果,请尝试回滚到生产的默认资产配置:

  # Disable Rails's static asset server (Apache or nginx will already do this).
  config.serve_static_assets = false

  # Compress JavaScripts and CSS.
  config.assets.js_compressor = :uglifier
  # config.assets.css_compressor = :sass

  # Do not fallback to assets pipeline if a precompiled asset is missed.
  config.assets.compile = true

  # Generate digests for assets URLs.
  config.assets.digest = true

  # Version of your assets, change this if you want to expire all your assets.
  config.assets.version = '1.0'

代替:

  #config.serve_static_assets = false
  config.serve_static_assets = true

  config.assets.compress = true
  config.assets.compile = true
  config.assets.precompile = ['*.js', 'application.css', 'styles.css.scss', '*.css.erb']
  config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif *.svg *.woff *.ttf *.ico)

  config.assets.digest = true

编辑 3:

编辑:

我刚刚在控制台中注意到图像已正确加载(我可以 加载http://website.com/assets/social_icons/facebook_ico.png),但不是 CSS+JS (http://****/assets/application-3b6ba7703e465259e4e7ebee17c5ea1e.js 404 未找到 - 与 .css 相同)

这也意味着您实际上并没有编译资产。如果是,您需要加载 facebook_ico.png,并在名称上附加哈希

【讨论】:

  • 您好,感谢您的留言。是的,每次部署后服务器都会重新启动 + 它指向 public 目录 + 权限也是正确的。
猜你喜欢
  • 2013-07-17
  • 1970-01-01
  • 2013-10-10
  • 1970-01-01
  • 2013-09-13
  • 1970-01-01
  • 1970-01-01
  • 2017-02-06
相关资源
最近更新 更多