1

我有一个让我发疯的 Rails 应用程序。我不知道为什么我会收到重复的 js 调用。我猜这与 application.js require 语句的顺序或组织以及 .js 文件的位置有关。我也尝试过使用 production.rb 并且通常只会让 .js 完全被它破坏。这是我所拥有的:

app
|assets
||javascripts
|||+data_centers
|||-data_center.js
||-application.js
||-rails.js


vendor
|assets
||javascripts
||-autocomplete-rails.js
||-jquery-1.9.1.js
||-jquery-ui-1.10.2.custom.js

application.js 有以下 require 语句:

//= require jquery-1.9.1
//= require jquery-ui-1.10.2.custom
//= require twitter/bootstrap
//= require bootstrap-typeahead
//= require rails
//= require autocomplete-rails

data_center.js 有以下 require 语句:

//= require highcharts
//= require highcharts/modules/canvas-tools
//= require highcharts/modules/exporting

之前,我有来自vendor/assetsin的 .js 文件,app/assets但被建议将所有外部 .js 保留在该vendor/assets位置。关键是那行不通。

我的 production.rb 看起来像这样:

MyApp::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

  # Full error reports are disabled and caching is turned on
  config.consider_all_requests_local = false
  #default 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
  #defualt false

  # Compress JavaScripts and CSS
  config.assets.compress = false
  #defualt true

  # Don't fallback to assets pipeline if a precompiled asset is missed
  config.assets.compile = true
  #default = false

  # Generate digests for assets URLs
  config.assets.digest = true

  # Defaults to nil and saved in location specified by config.assets.prefix
  # config.assets.manifest = YOUR_PATH

  # 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

  # See everything in the log (default is :info)
  # 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"

  # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
  # config.assets.precompile += %w( search.js )
  # config.assets.precompile += %w( jquery.js )
  # config.assets.precompile += %w( jquery_ujs.js )
  # config.assets.precompile += %w( jquery-ui-1.10.2.custom.js )
  # config.assets.precompile += %w( twitter/bootstrap.js )
  # config.assets.precompile += %w( bootstrap-typeahead.js )
  # config.assets.precompile += %w( rails.js )
  # config.assets.precompile += %w( autocomplete-rails.js )


  # Disable delivery errors, bad email addresses will be ignored
  # config.action_mailer.raise_delivery_errors = false

  # Enable threaded mode
  # config.threadsafe!

  # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
  # the I18n.default_locale when a translation can not be found)
  config.i18n.fallbacks = true

  # Send deprecation notices to registered listeners
  config.active_support.deprecation = :notify

  # Log the query plan for queries taking more than this (works
  # with SQLite, MySQL, and PostgreSQL)
  # config.active_record.auto_explain_threshold_in_seconds = 0.5
end

如果您有任何想法,请告诉我。谢谢。

4

1 回答 1

1

You could try config.assets.debug = true in your production.rb to get a better picture, but I'd suggest first trying to debug problems with assets in development.

Some gems or their dependencies may be loading assets that you might not have been aware of.

In Rails console in the environment you are interested in (i.e. you might need to ssh to the server, etc. and/or set the environment in/before the command):

rails c

you might use the following to get a little more info from Sprockets about current assets:

Rails.application.assets.each_file {|path| begin; Rails.application.assets[path].dependencies.each{|d| puts "#{path} dependencies: /assets/#{d.logical_path}"}; rescue; puts "#{path}"; end}

Then you might look at gem list in your prod environment or look at Gemfile.lock to see what gems bundler is using. Maybe assets are being loaded that you didn't know about.

For asset organization, see the guide.

Also, consider that the guide suggests some different settings than what you are using in your production config, e.g. it suggests using config.assets.compress = true and to set config.serve_static_assets = false and have apache or nginx serve static assets, for performance reasons.

于 2013-05-29T15:37:42.873 回答