Rails 应用不在生产环境中提供 JS 和 SCSS 资产

Rails app doesn't serve JS and SCSS assets in production environment

首先,在开发环境中一切正常。 但是,如果我切换到生产环境,我的 Rails 应用程序不提供 JS 和 CSS 资产。

我将资产部署到 public 目录:

 rake assets:precompile RAILS_ENV=production

没有错误。

日志说:

ActionController::RoutingError (No route matches [GET] "/assets/custom-9f5239947caa407b080981084efbe1b998006e083c7c79e950376483e1ac0f27.css"):

ActionController::RoutingError (No route matches [GET] "/assets/application-5e67105cb72dda5c0d9dcc451a325501380be971c097076180211f3e90a99ba3.js"):

assets.rb

Rails.application.config.assets.precompile += %w( cocoon.js )

production.rb

config.cache_classes = true
config.eager_load = true
config.consider_all_requests_local       = false
config.action_controller.perform_caching = true
config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
config.assets.js_compressor = :uglifier
config.assets.css_compressor = :sass
config.assets.compile = false
config.assets.digest = true
config.log_level = :debug
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
config.log_formatter = ::Logger::Formatter.new
config.active_record.dump_schema_after_migration = false

宝石文件

source 'https://rubygems.org'
gem 'carrierwave', '~> 0.10.0'
gem 'mini_magick'
gem 'minitest'
gem 'rails', '4.2.0'
gem 'rack'
gem 'sprockets'
gem 'therubyracer'
gem 'execjs'
gem 'sqlite3'
gem 'sass-rails', '~> 5.0'
gem 'sprockets'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'bcrypt'
gem 'bootstrap-sass', '~> 3.2.0.0'
gem 'bootstrap-will_paginate'
gem 'thin'
gem 'fog', '~> 1.23.0'
gem 'faker'
gem 'rails-i18n', github: 'svenfuchs/rails-i18n', branch: 'master' # For 4.x
gem 'simple_hashtag'
group :development, :test do
  gem 'byebug'
  gem 'web-console', '~> 2.0'
end
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'cocoon', '~> 1.2.0'
gem 'haml', '~> 4.0.6'

也许你可以试试这个:

Rails.application.config.assets.precompile += %w( custom.css ) # Because Asset Pipeline by default only compress application.(css|js)

还有这个:

config.serve_static_files = false

希望对你有用:D

在 production.rb 中我将 config.assets.compile 从 false 更改为 true:

config.assets.compile = true