我在 Heroku 上设置我的 Rails 应用程序,遇到了一个问题,我的所有 CSS 资产都已加载但我的 JS 资产都没有。我正在使用 memcached,并按照以下说明操作:https ://devcenter.heroku.com/articles/rack-cache-memcached-rails31 。
我注意到的一件事是,当我在 production.rb 文件中将 config.assets.compile 更改为“true”时,所有 JS 都已成功加载,但显然网站的初始加载速度非常慢。如何解决此问题?我是 Heroku 的新手,所以不确定,也没有在网上找到任何有用的东西。
我假设由于我所有的 JS 文件都在 /assets/javascripts/ 中,因此应该自动包含它们,但似乎它们不是。
这是我的production.rb:
BrainDb::Application.configure do
# Settings specified here will take precedence over those in config/application.rb
client = Dalli::Client.new
config.action_dispatch.rack_cache = {
:metastore => client,
:entitystore => client,
:allow_reload => false
}
# Code is not reloaded between requests
config.cache_classes = true
# Full error reports are disabled and caching is turned on
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = true
config.static_cache_control = "public, max-age=2592000"
# Compress JavaScripts and CSS
config.assets.compress = true
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false
# Generate digests for assets URLs
config.assets.digest = true
config.i18n.fallbacks = true
# Send deprecation notices to registered listeners
config.active_support.deprecation = :notify
end
我还从 public/assets 中删除了所有内容,以便进行预编译。这是application.rb:
require File.expand_path('../boot', __FILE__)
require 'rails/all'
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 BrainDb
class Application < Rails::Application
# Configure the default encoding used in templates for Ruby 1.9.
config.encoding = "utf-8"
# Configure sensitive parameters which will be filtered from the log file.
config.filter_parameters += [:password]
# Enable escaping HTML in JSON.
config.active_support.escape_html_entities_in_json = true
config.active_record.whitelist_attributes = true
config.assets.initialize_on_precompile = false
# Enable the asset pipeline
config.assets.enabled = true
config.cache_store = :dalli_store
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
结束结束
更新 Application.js:
//
//= require jquery
//= require jquery_ujs