Ruby-on-rails Rails 不会在生产中加载位于公共目录中的资产

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18806537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:43:08  来源:igfitidea点击:

Rails does not load assets located in public directory in production

ruby-on-railsassets

提问by Muflix

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

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

in layout i load

在布局中我加载

<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.

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

My Development.rb

我的发展.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

My Production.rb

我的作品.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

回答by sites

Configuration changed for Rails 4 and 5.

Rails 4 和 5 的配置已更改。

For Rails 4:

对于 Rails 4:

config.serve_static_files = true

For Rails 5:

对于 Rails 5:

config.public_file_server.enabled = true

回答by Stuart M

This is because you have

这是因为你有

  config.serve_static_assets = false

in your production.rbfile.

在您的production.rb文件中。

From the Rails Configuration guide:

Rails 配置指南

  • config.serve_static_assetsconfigures Rails itself to serve static assets. Defaults to true, but in the production environment is turned off as the server software (e.g. Nginx or Apache) used to run the application should serve static assets instead. Unlike the default setting set this to true when running (absolutely not recommended!) or testing your app in production mode using WEBrick. Otherwise you won′t be able use page caching and requests for files that exist regularly under the public directory will anyway hit your Rails app.
  • config.serve_static_assets配置 Rails 本身以提供静态资产。默认为 true,但在生产环境中关闭,因为用于运行应用程序的服务器软件(例如 Nginx 或 Apache)应改为提供静态资产。与默认设置不同,在运行时将此设置为 true(绝对不推荐!)或使用 WEBrick 在生产模式下测试您的应用程序。否则,您将无法使用页面缓存,并且对公共目录下定期存在的文件的请求无论如何都会命中您的 Rails 应用程序。

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.

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