使用 Rails 直接访问样式表
Direct access to stylesheet with Rails
我正在尝试启用对此文件的直接访问:http://example.com/assets/style.css
(位于 /assets/stylesheets/style.css
)
它在开发中有效,但在生产中出现 404 错误。
config/initializers/assets.rb:
Rails.application.config.assets.version = '1.0'
Rails.application.config.assets.precompile += %w( static_pages.css )
Rails.application.config.assets.precompile += %w( dashboard.css )
Rails.application.config.assets.precompile += %w( style.css )
config/initializers/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_assets = true
config.assets.js_compressor = :uglifier
config.assets.compile = true
config.assets.digest = true
config.force_ssl = false
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
因为 Rails Asset Pipeline 预编译资产并添加 fingerprint. You should use Asset Pipeline Helpers 而不是硬编码 http://example.com/assets/jquery-ui-slider-pips.css
.
如果你想在没有 Asset Pipeline 的情况下使用 JS 或 CSS 文件,只需将它们放入 public
目录(或 public
目录的任何子目录)并直接使用 link。即对于 public/style1.css
文件,您可以使用 URL http://example.com/style1.css
我正在尝试启用对此文件的直接访问:http://example.com/assets/style.css
(位于 /assets/stylesheets/style.css
)
它在开发中有效,但在生产中出现 404 错误。
config/initializers/assets.rb:
Rails.application.config.assets.version = '1.0'
Rails.application.config.assets.precompile += %w( static_pages.css )
Rails.application.config.assets.precompile += %w( dashboard.css )
Rails.application.config.assets.precompile += %w( style.css )
config/initializers/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_assets = true
config.assets.js_compressor = :uglifier
config.assets.compile = true
config.assets.digest = true
config.force_ssl = false
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
因为 Rails Asset Pipeline 预编译资产并添加 fingerprint. You should use Asset Pipeline Helpers 而不是硬编码 http://example.com/assets/jquery-ui-slider-pips.css
.
如果你想在没有 Asset Pipeline 的情况下使用 JS 或 CSS 文件,只需将它们放入 public
目录(或 public
目录的任何子目录)并直接使用 link。即对于 public/style1.css
文件,您可以使用 URL http://example.com/style1.css