jQuery on rails在heroku& amp;雪松

时间:2012-06-17 09:00:30

标签: jquery ruby-on-rails jquery-ui heroku jquery-ui-accordion

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

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

我正在努力让两件事与javascripts有关:

  • jQuery close函数 - 和 -
  • 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 install):

# 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

development.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 dev工具查看时)。生产中只显示了2个javascripts,两者都有错误,如下所示:

的application.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-plater:)

1 个答案:

答案 0 :(得分:2)

我解决了这个问题与rails资产管道有关。

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

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

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

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