jQuery と jQuery UI の両方がローカルで動作していますが、本番環境 (Cedar/Heroku が役立つ場合) での動作を拒否しているだけです。
私はこれについて多くの調査を行いましたが、まだ困惑しており、誰かが私を助けてくれることを望んでいます.
私はjavascriptに関して2つのことを働かせようとしています:
- 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
以下のフィードバックのおかげで、私の開発環境では、すべての JavaScript がロードされていて、エラーがないことに気付きました (スクリプトの下で Chrome 開発ツールを調べた場合)。本番環境で表示される JavaScript は 2 つだけで、両方に以下のようなエラーがあります。
アプリケーション.js
Uncaught TypeError: Cannot read property 'fn' of undefined
jquery-ui.js
Uncaught ReferenceError: jQuery is not defined
.erb html ファイルでは、スクリプトを実行しようとしたときのエラーを次に示します ( *は、クロムで赤で表示される場所を示すために追加されています)。
<script>
$(function() {
****** Uncaught ReferenceError: $ is not defined *********
$( "#accordion" ).accordion();
});
</script>
何か案は?これは Rails アセット パイプラインの問題ですか、それとも他の問題ですか? heroku ログを実行しても、詳細なエラー メッセージが表示されません。
RailsのL-platerから事前に感謝します:)