【问题标题】:Rails Heroku Precompiled Assets Not Loading JS?Rails Heroku 预编译资产不加载 JS?
【发布时间】:2013-08-30 22:09:15
【问题描述】:

我在 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'

结束 结束

更新 应用程序.js:

//
//= require jquery
//= require jquery_ujs

【问题讨论】:

  • 你能发布你的 assets/application.js 文件吗?这个问题的答案就在那里。
  • 我用 application.js 文件中的所有内容更新了它。文件顶部的其他所有内容都只是关于如何使用文件的 cmets。

标签: javascript ruby-on-rails ruby ruby-on-rails-3 heroku


【解决方案1】:

添加

//= require_tree . 最后并预编译

在您的 application.js 中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-12
    • 2014-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多