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

查看:75
本文介绍了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文件中.

轨道配置指南:

  • config.serve_static_assets 将Rails本身配置为提供静态资产.默认为true,但是在生产环境中已关闭,因为用于运行该应用程序的服务器软件(例如Nginx或Apache)应改为提供静态资产.与默认设置不同,在运行(绝对不建议!)或使用WEBrick在生产模式下测试您的应用程序时,请将其设置为true.否则,您将无法使用页面缓存,并且对公共目录下定期存在的文件的请求将仍然会影响您的Rails应用.
  • config.serve_static_assets configures 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.

就像该指南所建议的那样,您真的不应该依赖通过Rails应用程序从public/提供服务的资产,最好让Web服务器(例如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天全站免登陆