2

我有 jQuery 和 jQuery UI 都在本地工作,但他们只是拒绝在我的生产环境中工作(如果有帮助的话,Cedar/Heroku)。

我对此进行了大量研究,但仍然感到困惑,希望有人能帮助我。

我正在尝试使有关 javascripts 的两件事起作用:

  • jQuery 关闭功能 - 和 -
  • jQuery UI 手风琴

在 index.html.erb 文件中调用的脚本:

<script>
    $(function() {
$( "#accordion" ).accordion();
});
</script>

<div class="demo">
<h2>JQuery UI Accordion - works in development, but not production</h2><br>
<div id="accordion">
<h3><a href="#">Section 1</a></h3>
   <div>
      <p>
        (text removed).
      </p>
   </div>
<h3><a href="#">Section 2</a></h3>


<div class="alert alert-success">
   <button type="button" class="close" data-dismiss="alert">×</button>
   <strong>Click the X in the right of this box. </strong> It should close in production, but it doesn't. Yet it closes in development - strange!<br>
</div>

在 application.html.erb 中调用 jQuery:

<%= javascript_include_tag "application" %>
<%= javascript_include_tag "jquery-ui" %>

这是 application.js:

// This is a manifest file that'll be compiled into including all the files listed below.
// Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
// be included in the compiled file accessible from http://example.com/assets/application.js
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
//= require_tree .

在我的 /assets/javascripts 目录中存在以下文件:jquery-1.7.2.min.js、jquery-us-1.8.20.custom.min.js 和 jquery.ui.accordion.js

它们也可以通过手动加载它们的 URL 来找到(所以我已经确认它们是在 git 推送期间上传的)。

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 Aqua
  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 the asset pipeline
    config.assets.enabled = true

    # Version of your assets, change this if you want to expire all your assets
    config.assets.version = '1.0'
    config.assets.initialize_on_precompile = false #added as per rubyguides 4.1 asset pipeline
  end
end

正在调用显示 jQuery 的 Gemfile(已运行捆绑安装):

# Bundle edge Rails instead:
# gem 'rails',     :git => 'git://github.com/rails/rails.git'

#gem 'mysql2' removed for pg required on Heroku
gem 'pg' #added as per Heroku requirements
gem 'omniauth'
gem 'omniauth-twitter'
gem 'omniauth-facebook'
gem 'omniauth-openid', :git => 'git://github.com/intridea/omniauth-openid.git'
gem 'thin'

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.1.5'
  gem 'coffee-rails', '~> 3.1.1'
  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

(removed a bunch of commented out fields)    

group :test do
  # Pretty printed test output
  gem 'turn', '~> 0.8.3', :require => false
end

开发.rb:

Aqua::Application.configure do
  # Settings specified here will take precedence over those in config/application.rb

  # In the development environment your application's code is reloaded on
  # every request.  This slows down response time but is perfect for development
  # since you don't have to restart the web server when you make code changes.
  config.cache_classes = false

  # Log error messages when you accidentally call methods on nil.
  config.whiny_nils = true

  # Show full error reports and disable caching
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false

  # Don't care if the mailer can't send
  config.action_mailer.raise_delivery_errors = false

  # Print deprecation notices to the Rails logger
  config.active_support.deprecation = :log

  # Only use best-standards-support built into browsers
  config.action_dispatch.best_standards_support = :builtin

  # Do not compress assets
  config.assets.compress = false

  # Expands the lines which load the assets
  config.assets.debug = true
end

最后是production.rb:

Aqua::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
  config.action_controller.perform_caching = true

  # Disable Rails's static asset server (Apache or nginx will already do this)
  config.serve_static_assets = true

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

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

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

  # Defaults to Rails.root.join("public/assets")
  # 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

  # Use a different logger for distributed setups
  # config.logger = 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 )

  # 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
end

感谢下面的反馈,我注意到在我的开发环境中我加载了所有 javascripts 并且没有错误(通过脚本下的 chrome 开发工具查看时)。生产中只显示了 2 个 javascript,并且都有如下错误:

应用程序.js

Uncaught TypeError: Cannot read property 'fn' of undefined

jquery-ui.js

Uncaught ReferenceError: jQuery is not defined

在 .erb html 文件中,这是脚本尝试运行时的错误(添加*以显示它在 chrome 中以红色显示的位置):

<script>
    $(function() {
******  Uncaught ReferenceError: $ is not defined *********
        $( "#accordion" ).accordion();
    });
    </script>

有任何想法吗?这是 Rails 资产管道问题还是其他问题?运行 heroku 日志并没有给我任何详细的错误消息。

提前感谢 Rails 上的 L-plate :)

4

1 回答 1

2

我发现这个问题与 Rails 资产管道有关。

我仍然不完全确定它为什么会发生,但是 application.js 错误地编译了 jquery (等)。

我的短期解决方法(实际上并没有那么麻烦)是将每个单独的 javascript 文件手动添加到我的主布局 html.erb 页面中,然后从那里过滤到彼此中。

当然,它涉及更多单独的负载,但我的网站容量不高,性能也没有明显下降。

一旦我对 rails 管道有了更好的了解,我就会回去永久地对其进行整理。

于 2012-06-18T11:03:26.800 回答