Rails 不会在生产中加载位于公共目录中的资产 [英] Rails does not load assets located in public directory in production

查看:39
本文介绍了Rails 不会在生产中加载位于公共目录中的资产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我在公共目录中有资产(因为简单)

Hello i have assets in public directory (because of simplicity)

在我加载的布局中

<link href="/bootstrap/css/bootstrap.css" rel="stylesheet">
<link href="/assets/css/jumbotron.css" rel="stylesheet">
<link href="/assets/css/application.css" rel="stylesheet">

在开发中它运行良好,但在生产中未加载资产.

and in Development it works well but in Production assets are not loaded.

我的Development.rb

Web::Application.configure do
  config.cache_classes = false
  config.whiny_nils = true
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false
  config.action_mailer.raise_delivery_errors = false
  config.active_support.deprecation = :log
  config.action_dispatch.best_standards_support = :builtin
  config.active_record.mass_assignment_sanitizer = :strict
  config.active_record.auto_explain_threshold_in_seconds = 0.5
  config.assets.compress = false
  config.assets.debug = true
end

我的Production.rb

Web::Application.configure do
  config.cache_classes = false
  config.consider_all_requests_local       = true # default false, zobrazuje errory
  config.action_controller.perform_caching = false # default true
  config.serve_static_assets = false
  config.assets.compress = true
  config.assets.compile = true # default false
  config.assets.digest = true
  config.i18n.fallbacks = true
  config.active_support.deprecation = :notify
end

推荐答案

这是因为你有

  config.serve_static_assets = false

在您的 production.rb 文件中.

来自 Rails 配置指南:

  • config.serve_static_assets 配置 Rails 本身以提供静态资产.默认为 true,但在生产环境中关闭,因为用于运行应用程序的服务器软件(例如 Nginx 或 Apache)应改为提供静态资产.与默认设置不同,在运行时(绝对不推荐!)或使用 WEBrick 在生产模式下测试您的应用程序时将此设置为 true.否则,您将无法使用页面缓存,并且对公共目录下定期存在的文件的请求无论如何都会命中您的 Rails 应用.

就像那个指南所建议的那样,你真的不应该依赖于通过你的 Rails 应用程序从 public/ 提供资产,最好让网络服务器(例如 Apache 或 Nginx)处理服务资产性能.

And like that guide suggests, you really shouldn't rely on serving assets from public/ via your Rails app, it is better to let the web server (e.g. Apache or Nginx) handle serving assets for performance.

这篇关于Rails 不会在生产中加载位于公共目录中的资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆