EDIT: It looks like for whatever reason my assets are not loading. My javascript is returning me 404 errors on the server.
ORIGINAL: I know jQuery loads when I run the app on my localhost because; 1. my app works, and 2. the following code also tells me jQuery is loading when the page loads.
if (typeof jQuery != 'undefined') {
// jQuery is loaded => print the version
alert(jQuery.fn.jquery);
}
The problem is as soon as I push it to Heroku, jQuery doesn't work. The above code doesn't run anymore, and when I try to run $.post( "/time_records", {current_time: current_time}); i get the following error; ReferenceError: $ is not defined
When I run my Bundler on Heroku it says Using jquery-rails (3.1.0)
Application.js
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
GemFile
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.2'
# Use postgresql as the database for Active Record
gem 'pg'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'
group :doc do
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', require: false
end
# Use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.1.2'
# Use unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]
Since you're putting your application online on Heroku, check if the Javascript file doesn't return a 404 HTTP error online. If it does, jQuery will never be loaded...
It can be due to severals errors in your configuration (absolute filepath from your local configuration or whatever)
(Thanks to @Maxime for pointing me in the right direction.)
According to the Heroku Rails Support page, as a final task in the compilation phase, the assets:precompile Rake task is executed. If you have a assets:precompile Rake task defined, and don’t have a public/assets/manifest-*.json file. This will compile all assets and put them in your public directory. If this asset compilation fails, the deploy will fail as well.
My deploy was not failing, so I thought this step was happening smoothly, but when I checked the logs it said
-----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
Asset precompilation completed (2.49s)
Cleaning assets
Running: rake assets:clean
-----> WARNINGS:
Include 'rails_12factor' gem to enable all platform features
It turns out that without the inclusion of the rails_12factor gem the pages were not able to find the assets properly, and returning 404 errors. The fix was simple, add the following to your GemFile;
group :production do
gem 'rails_12factor'
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With