我在 Rails 4 中遇到了资产管道问题。我在testapp/public/assets/
有一个目录,其中包含所有散列或指纹化的 CSS 和 JavaScript bootstrap.min-58853fb115aba672ce98597de33da61b.js OR animate-d81627498b7ff4acf1a1964da02db789.css
但是在浏览器中我没有看到像<link rel="stylesheet" media="all" href="/stylesheets/bootstrap.min.css">
这样的散列 CSS 和 JavaScript 我需要在我的公共/资产/中请求散列文件
我有着陆控制器和一种方法视图/着陆/一个.html.erb
<%= render "shared/headmeta" %>
<%= render "shared/headcss"%>
<%= render "shared/headscript"%>
<%= render "shared/header"%>
<h1>Landing#one</h1>
<p>Find me in app/views/landing/new.html.erb</p>
<%= render "shared/footer"%>
<%= render "shared/jscript"%
>
CSS manifest in landing.scss
/* ...
*= require animate
*= require bootstrap.min
*= require default
*= require style
*= require overwrite
*/
headcss 部分是:
<!-- Bootstrap core CSS -->
<%= stylesheet_link_tag "bootstrap.min", media: "all", "data-turbolinks-track" => true %>
<!-- Overwrite bootstrap style -->
<%= stylesheet_link_tag "overwrite", media: "all","data-turbolinks-track" => true %>
<!-- Animate css -->
<%= stylesheet_link_tag "animate", media: "all","data-turbolinks-track" => true %>
<!-- Heading font -->
<%= stylesheet_link_tag "stylesheet", media: "all","data-turbolinks-track" => true %>
<!-- General font -->
<%= stylesheet_link_tag "http://fonts.googleapis.com/css?family=Open+Sans", media: "all" ,"data-turbolinks-track" => true%>
<!-- Font icons -->
<%= stylesheet_link_tag "simple-line-icons", media: "all","data-turbolinks-track" => true %>
<!-- Flexslider -->
<%= stylesheet_link_tag "flexslider", media: "all","data-turbolinks-track" => true %>
<!-- Custom styles for this template -->
<%= stylesheet_link_tag "style", media: "all","data-turbolinks-track" => true %>
<!-- Theme skin -->
<%= stylesheet_link_tag "default", media: "all","data-turbolinks-track" => true %>
我的宝石文件
source 'https://rubygems.org'
#gem devise
gem 'devise'
#sprockets is a rack-based asset packaging system that concatenates and serves Js,coffescript,css,LESS
gem 'sprockets', '~> 2.12.3'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.0'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use yui as compressor for JavaScript assets
gem 'yui-compressor', '~> 0.12.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
end
生产.rb
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# Code is not reloaded between requests.
config.cache_classes = true
# Eager load code on boot. This eager loads most of Rails and
# your application in memory, allowing both threaded web servers
# and those relying on copy on write to perform better.
# Rake tasks automatically ignore this option for performance.
config.eager_load = true
# Full error reports are disabled and caching is turned on.
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
# Enable Rack::Cache to put a simple HTTP cache in front of your application
# Add `rack-cache` to your Gemfile before enabling this.
# For large-scale production use, consider using a caching reverse proxy like
# NGINX, varnish or squid.
# config.action_dispatch.rack_cache = true
# Disable serving static files from the `/public` folder by default since
# Apache or NGINX already handles this.
config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
# Compress JavaScripts and CSS.
config.assets.js_compressor = :uglifier
config.assets.css_compressor = :yui
# Do not fallback to assets pipeline if a precompiled asset is missed.
config.assets.compile = true
# Asset digests allow you to set far-future HTTP expiration dates on all assets,
# yet still be able to expire them through the digest params.
config.assets.digest = true
# `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb
# Specifies the header that your server uses for sending files.
# config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
# config.force_ssl = true
# Use the lowest log level to ensure availability of diagnostic information
# when problems arise.
config.log_level = :debug
# Prepend all log lines with the following tags.
# config.log_tags = [ :subdomain, :uuid ]
# Use a different logger for distributed setups.
# config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
# Use a different cache store in production.
# config.cache_store = :mem_cache_store
# Enable serving of images, stylesheets, and JavaScripts from an asset server.
# config.action_controller.asset_host = 'http://assets.example.com'
# Ignore bad email addresses and do not raise email delivery errors.
# Set this to true and configure the email server for immediate delivery to raise delivery errors.
# config.action_mailer.raise_delivery_errors = false
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation cannot be found).
config.i18n.fallbacks = true
# Send deprecation notices to registered listeners.
config.active_support.deprecation = :notify
# Use default logging formatter so that PID and timestamp are not suppressed.
config.log_formatter = ::Logger::Formatter.new
# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false
end
config/application.rb
require File.expand_path('../boot', __FILE__)
require 'rails/all'
require "sprockets/railtie"
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module Marketpersona
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
# Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks = true
config.assets.enabled = true
end
end
config/initializers/assets.rb
# Be sure to restart your server when you modify this file.
# Version of your assets, change this if you want to expire all your assets.
Rails.application.config.assets.version = '1.0'
# Add additional assets to the asset load path
# Rails.application.config.assets.paths << Emoji.images_path
Rails.application.config.assets.compress = true
# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in app/assets folder are already added
#Rails.application.config.assets.precompile << Proc.new do |path|
# if path =~ /.(css|js)z/
# full_path = Rails.application.assets.resolve(path).to_path
# app_assets_path = Rails.root.join('app', 'assets').to_path
#if full_path.starts_with? app_assets_path
#Rails. logger.info "including asset: " + full_path
#true
#else
# Rails.logger.info "excluding asset: " + full_path
# false
#end
#else
# false
#end
#end
Rails.application.config.assets.precompile = ['*.js', '*.css', '*.css.erb']
某些文件的公共/资产输出
/public/assets# ls
animate-d2e7ae01911a05d8ce767b98def4ae3a.css jquery.smooth-scroll-5f54331165eef2a11b63f6d615d4556e.js.gz
animate-d2e7ae01911a05d8ce767b98def4ae3a.css.gz jquery.ui.totop-d23c655a6d2cb2716708132fbd150f5e.js
animate-d81627498b7ff4acf1a1964da02db789.css jquery.ui.totop-d23c655a6d2cb2716708132fbd150f5e.js.gz
animate-d81627498b7ff4acf1a1964da02db789.css.gz jquery.ui.totop-d5647d1f3ae94dc679d5767720d0ab73.js
application-88198fbec2cb093a4a5545b36197d203.js jquery.ui.totop-d5647d1f3ae94dc679d5767720d0ab73.js.gz
application-88198fbec2cb093a4a5545b36197d203.js.gz jquery_ujs-0b8f09cf5aec4ba1be1abd9f915c77e2.js
application-9ebac1bd19f3ba981afd79fd2c5f2037.css jquery_ujs-0b8f09cf5aec4ba1be1abd9f915c77e2.js.gz
application-9ebac1bd19f3ba981afd79fd2c5f2037.css.gz landing-307af29ac27a030829bb53a3718d3987.js
application-a6b3011ab79b9f60d493732939ce4f50.css landing-307af29ac27a030829bb53a3718d3987.js.gz
application-a6b3011ab79b9f60d493732939ce4f50.css.gz landing-7c3fe69fe5644297d4d87fdcc4b915d4.css
application-aceb45182f1f0486e0e79c02dea65c4e.css landing-7c3fe69fe5644297d4d87fdcc4b915d4.css.gz
application-aceb45182f1f0486e0e79c02dea65c4e.css.gz landing-8f6eecd1178ef85964369ec66d33c4f1.js
application-d012cec9935d5b05e1b812ccc6f3cf6d.css landing-8f6eecd1178ef85964369ec66d33c4f1.js.gz
application-d012cec9935d5b05e1b812ccc6f3cf6d.css.gz landing-9b1e28a2786f16d823f619cdc65d8a40.css
application-e038cd04ae9494b6950481cfc5d4d749.css landing-9b1e28a2786f16d823f619cdc65d8a40.css.gz
application-e038cd04ae9494b6950481cfc5d4d749.css.gz landing-a7374c322246655f8a4a6d05689e50ae.css
application-e1d3507ada82fdd954d492f952b28619.js landing-a7374c322246655f8a4a6d05689e50ae.css.gz
application-e1d3507ada82fdd954d492f952b28619.js.gz landing-d728d395d4bd4966f84f4acdd1d6691c.css
bootstrap.min-58853fb115aba672ce98597de33da61b.js landing-d728d395d4bd4966f84f4acdd1d6691c.css.gz
您有一个页面/应用程序中包含 9 个样式表?为什么不使用清单将它们编译成一个?
我真的不确定这里发生了什么...因此,您没有在生产环境中获取已编译的文件?
我可能误读了您的问题,但是您是否正在创建自己的css清单文件,而不是应用程序.css,例如资产/样式表/共享/?然后在你的 production.rb(环境)文件中,你需要告诉 Rails 编译它们:
AppName::Application.configure do
# application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
config.assets.precompile += ['shared/filename.css', 'shared2/filename2.css']
end
运行RAILS_ENV=production bundle exec rake assets:precompile
然后添加、提交更改并将其部署到生产环境。