Rails不会加载位于生产中的公共目录中的资产

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

在布局我加载

   

在开发中它运作良好,但在生产资产没有加载。

我的发展.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应用程序。

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

Rails 4和5的配置已更改。

对于Rails 4:

 config.serve_static_files = true 

对于Rails 5:

 config.public_file_server.enabled = true