jQuery on Rails 无法在 Heroku 和 cedar 的生产环境中工作

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

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

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

我正在尝试让有关 javascript 的两件事发挥作用:

  • jQuery 关闭函数 - 和 -
  • jQuery UI Accordion

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

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

# 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

最后, 生产.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

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

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 Assets 管道问题还是其他问题?运行heroku日志没有给我任何详细的错误消息。

提前感谢来自 Rails 上的 L 盘:)

最佳答案

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

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

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

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

一旦我对 Rails 管道有了更好的了解,我就会回去永久地解决它。

关于jQuery on Rails 无法在 Heroku 和 cedar 的生产环境中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11070123/

相关文章:

jquery - 将 Jquery Accordion 扩展到表的行

javascript - 即使鼠标移开后,悬停下拉菜单仍保持事件状态

javascript - JS/jQuery 替换部分日期字符串不起作用

ruby-on-rails - DirtyAttributes 采用更改后的 BigDecimal 类型

javascript - 如何将 jQuery UI 所有选定范围(不仅是最小值和最大值)加载到数组中

javascript - jquery draggable droppable remove 掉落

php - AJAX 发布不工作,数据似乎没有通过

ruby-on-rails - 使用rails命令 "rake db:create:all"时出错 - 我认为它与Ruby的版本冲突有关

iphone - 使用 apn_on_rails 记录 Apple 推送通知服务器的响应?

jquery - 如何创建定期更改颜色的多色文本字符串(如拉斯维加斯横幅)