rails - 在生产模式下找不到application.css资产

问题描述 投票:27回答:4

我正在升级应用程序以使用资产管道。

我已经将css资产编译成应用程序css文件但是当我在生产模式下运行应用程序时找不到它们

RAILS_ENV=production bundle exec rails s

我访问任何页面,我从数据库中获得正确的输出,但没有样式和日志显示:

ActionController::RoutingError (No route matches [GET] 
"/assets/default.scss-1a27c...f07c.css"):

即使该文件存在于公共/资产中

$ ls public/assets/def*
public/assets/default.scss-1a27c...f07c.css     public/assets/default.scss.css
public/assets/default.scss-1a27c...f07c.css.gz  public/assets/default.scss.css.gz

我需要更改什么才能让服务器找到资产文件?

我的其他.css文件也是如此。 他们用指纹编译成公共/资产,但后来找不到。

页面来源显示:

<link href="/assets/default.scss-1a27c...f07c.css" 
media="screen" rel="stylesheet" type="text/css" />

rails(haml)source是= stylesheet_link_tag 'default.scss.css'

public.assets curently包括具有下列文件。

$ ls public/assets/def*
public/assets/default.scss-1a27c22229b7b522066181f27af4f07c.css
public/assets/default.scss-1a27c22229b7b522066181f27af4f07c.css.gz
public/assets/default.scss.css
public/assets/default.scss.css.gz

application.rb有

$ cat config/application.rb 
require File.expand_path('../boot', __FILE__)

# Pick the frameworks you want:
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "sprockets/railtie"
# require "rails/test_unit/railtie"

if defined?(Bundler)
  # If you precompile assets before deploying to production, use this line
  Bundler.require(*Rails.groups(:assets => %w(development test)))
  # If you want your assets lazily compiled in production, use this line
  # Bundler.require(:default, :assets, Rails.env)
end

module Linker
  class Application < Rails::Application
    config.encoding = "utf-8"
    config.filter_parameters += [:password]
    config.assets.enabled = true
    config.assets.initialize_on_precompile = false # For Heroku
    config.assets.version = '1.0'
  end
end

config/environments/production具有:

$ cat config/environments/production.rb 
Linker::Application.configure do
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true
  config.assets.precompile += ['default.scss.css','main.css', 'jquery-ui-1.8.22.custom.css']
  config.serve_static_assets = false
  config.assets.compress = true
  config.assets.compile = false
  config.assets.digest = true
  config.log_level = :debug
  config.i18n.fallbacks = true
  config.active_support.deprecation = :notify
end

这似乎发生在所有资产上,例如

Started GET "/assets/default.scss-1a27c22229b7b522066181f27af4f07c.css" for 127.0.0.1 at 2014-02-23 10:24:47 -0500
ActionController::RoutingError (No route matches [GET] "/assets/default.scss-1a27c22229b7b522066181f27af4f07c.css"):
Started GET "/assets/main-6864687b4114a1c316e444bd90f233ff.css" for 127.0.0.1 at 2014-02-23 10:24:47 -0500
ActionController::RoutingError (No route matches [GET] "/assets/main-6864687b4114a1c316e444bd90f233ff.css"):
Started GET "/assets/jquery-ui-1.8.22.custom-24319b4b1218846a3fe22a0479ae98b4.css" for 127.0.0.1 at 2014-02-23 10:24:47 -0500
ActionController::RoutingError (No route matches [GET] "/assets/jquery-ui-1.8.22.custom-24319b4b1218846a3fe22a0479ae98b4.css"):
Started GET "/assets/application-fc1d492d730f2a45581a40eac4607db8.js" for 127.0.0.1 at 2014-02-23 10:24:47 -0500
ActionController::RoutingError (No route matches [GET] "/assets/application-fc1d492d730f2a45581a40eac4607db8.js"):
Started GET "/images/link.ico" for 127.0.0.1 at 2014-02-23 10:24:48 -0500
ActionController::RoutingError (No route matches [GET] "/images/link.ico"):
css ruby-on-rails ruby-on-rails-3 asset-pipeline
4个回答
31
投票

默认情况下,Rails不会为public资产提供服务。 看你的production.rb

  config.serve_static_assets = true

改变为真,你很高兴。 (注:你不希望成为true生产,记得在部署前将它改回来!)

有关详细信息,请参阅配置Rails应用

在rails 6中 ,在默认的production.rb中应该有一行

config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?

所以运行你的服务器

RAILS_SERVE_STATIC_FILES=true rails server -e production

或在production.rb设置config.public_file_server.enabled=true 。 有关导轨4和5,请参见下面的答案。


10
投票

Rails 5解决方案类似于Jules Copeland上面给出的Rails 4解决方案

在预先生成的config/environments/production.rb文件中,应该有一个如下所示的条目:

# Disable serving static files from the `/public` folder by default since
# Apache or NGINX already handles this.
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?

我在http://guides.rubyonrails.org配置Rails应用程序指南中找到了对此设置的一个不错的解释:

config.public_file_server.enabled将Rails配置为从公共目录提供静态文件。 此选项默认为true,但在生产环境中,它设置为false,因为用于运行应用程序的服务器软件(例如NGINX或Apache)应该提供静态文件。 如果您使用WEBrick在生产模式下运行或测试应用程序(建议不要在生产中使用WEBrick),请将该选项设置为true。 否则,您将无法使用页面缓存并请求公共目录下存在的文件。

结论 :在生产中,使用RAILS_SERVE_STATIC_FILES=1启动rails服务器将允许Rails像web服务器一样提供public / assets目录中的任何文件。 请记住,Rails是一个应用服务器,不会像Web服务器那样高效(例如NGINX,Apache等)。 对于真实世界的应用程序,您应该在Rails前面有一个专用的Web服务器,它将自行提供静态资产,并且只根据需要为动态内容打扰Rails。 有关更多详细信息,请参阅Justin Weiss关于Web服务器和应用服务器之间差异的文章


9
投票

在Rails 4中,您可以通过传递环境变量让它们在生产中显示(在本地运行):

RAILS_SERVE_STATIC_FILES=true rails server -e production

只要在/config/environments/production.rb有这一行,这应该可以工作:

config.serve_static_files = ENV ['RAILS_SERVE_STATIC_FILES']。现在?


4
投票

当您执行rake assets:precompile ,您的资产将进入公共目录。 看看你是否可以在public/assets/找到这些文件

你应该看到这样的东西:

I, [2014-02-23T20:06:21.853314 #26915]  INFO -- : Writing app_root/public/assets/application-ecd8636fc80ea2b712039c4abc365da9.css
© www.soinside.com 2019 - 2024. All rights reserved.