jquery not working in production?

Hello all,

I seem to be running into an issue where jquery seems to be working fine in my development environment but when I load it to my production environment it does not work. There are some menus that I am trying to hide when the web page loads but when they page loads they are there in plain site. When I launch firebug and run the same code in console it works without any issue so I have wonder if there is a different way I need to precompile the assets to get everything to load?

Here is what my environment is like:

Rails Version: 3.2.3 Ruby Version: 1.9.3 (RVM)

Gemfile … gem ‘jquery-rails’, ‘~> 2.0.1’ …

app/assets/javascripts/application.js … //= require jquery //= require jquery_ujs //= require_tree .

app/assets/javascripts/custom.js $(function(){ $(‘#roles_menu’).hide(); $(‘#issues_menu’).hide(); $(‘#features_menu’).hide(); $(‘#roles_menu_link’).click( function(){ if($(‘#roles_menu’).is(‘:hidden’)){ $(‘#roles_menu’).slideDown(‘slow’); // $(‘#roles_menu_link’).html(“hide roles menu”); } else{ $(‘#roles_menu’).slideUp(‘slow’); // $(‘#roles_menu_link’).html(“show roles menu”); } });

$('#issues_menu_link').click(
    function(){
        if($('#issues_menu').is(':hidden')){
            $('#issues_menu').slideDown('slow');
            // $('#issues_menu_link').html("<a href='#' id='issues_menu_link'>hide issues menu</a>");
        } else{
            $('#issues_menu').slideUp('slow');
            // $('#issues_menu_link').html("<a href='#' id='issues_menu_link'>show issues menu</a>");
        }
    });

    $('#features_menu_link').click(
        function(){
            if($('#features_menu').is(':hidden')){
                $('#features_menu').slideDown('slow');
                // $('#features_menu_link').html("<a href='#' id='features_menu_link'>hide features menu</a>");
            } else{
                $('#features_menu').slideUp('slow');
                // $('#features_menu_link').html("<a href='#' id='features_menu_link'>show features menu</a>");
            }
        });

});

Hello all,

I seem to be running into an issue where jquery seems to be working fine in my development environment but when I load it to my production environment it does not work. There are some menus that I am trying to hide when the web page loads but when they page loads they are there in plain site. When I launch firebug and run the same code in console it works without any issue so I have wonder if there is a different way I need to precompile the assets to get everything to load?

UPDATE: interestingly enough I ran rake assets:precompile and I didn’t get any errors prior to submitting this topic. I decided to run rake assets:precompile:all and I hit an error:

rake aborted! couldn’t find file ‘jquery’

To make sure I ran rake assets:precompile again to see if I wouldn’t get any errors and I again hit another error but it is different from the error I got above:

rake aborted! Invalid CSS after " background:url(": expected expression (e.g. fr, 2n+1), was “<%= asset_path …”

I though the jquery-rails gem was supposed to load jquery for me. Is it that I don’t need to include jquery and jquery_ujs in my application.js file?

Hello all,

I seem to be running into an issue where jquery seems to be working fine in my development environment but when I load it to my production environment it does not work. There are some menus that I am trying to hide when the web page loads but when they page loads they are there in plain site. When I launch firebug and run the same code in console it works without any issue so I have wonder if there is a different way I need to precompile the assets to get everything to load?

UPDATE: interestingly enough I ran rake assets:precompile and I didn’t get any errors prior to submitting this topic. I decided to run rake assets:precompile:all and I hit an error:

rake aborted! couldn’t find file ‘jquery’

To make sure I ran rake assets:precompile again to see if I wouldn’t get any errors and I again hit another error but it is different from the error I got above:

rake aborted! Invalid CSS after " background:url(": expected expression (e.g. fr, 2n+1), was “<%= asset_path …”

I though the jquery-rails gem was supposed to load jquery for me. Is it that I don’t need to include jquery and jquery_ujs in my application.js file?

UPDATE 2: Changing my css file to css.erb fixed my css error but I am now back to my jquery error. I have confirmed that when I run rake assets:precompile I do not get an error about jquery but when I run rake assets:precompile:all I get an error about jquery. What would make it so that one complains the other does not? and also do I need to include jquery and jquery_ujs if I am using the jquery-rails gem?

Val,

Your Gemfile looks good to me and so does your application.js.

Are you using bundle exec rake to precompile? Can you post the error message you get about jquery?

Hello all,

I seem to be running into an issue where jquery seems to be working fine in my development environment but when I load it to my production environment it does not work. There are some menus that I am trying to hide when the web page loads but when they page loads they are there in plain site. When I launch firebug and run the same code in console it works without any issue so I have wonder if there is a different way I need to precompile the assets to get everything to load?

UPDATE: interestingly enough I ran rake assets:precompile and I didn’t get any errors prior to submitting this topic. I decided to run rake assets:precompile:all and I hit an error:

rake aborted! couldn’t find file ‘jquery’

To make sure I ran rake assets:precompile again to see if I wouldn’t get any errors and I again hit another error but it is different from the error I got above:

rake aborted! Invalid CSS after " background:url(": expected expression (e.g. fr, 2n+1), was “<%= asset_path …”

I though the jquery-rails gem was supposed to load jquery for me. Is it that I don’t need to include jquery and jquery_ujs in my application.js file?

UPDATE 2: Changing my css file to css.erb fixed my css error but I am now back to my jquery error. I have confirmed that when I run rake assets:precompile I do not get an error about jquery but when I run rake assets:precompile:all I get an error about jquery. What would make it so that one complains the other does not? and also do I need to include jquery and jquery_ujs if I am using the jquery-rails gem?

Val,

Your Gemfile looks good to me and so does your application.js.

Are you using bundle exec rake to precompile? Can you post the error message you get about jquery?

Thanks for the response Ylan. I just now tried bundle exec rake assets:precompile:all and I am getting the same error about jquery. The error I am getting is:

rake aborted! couldn’t find file ‘jquery’ (in /home/vmcilwain/apps/test_app/app/assets/javascripts/application.js:13)

Tasks: TOP => assets:precompile:primary (See full trace by running task with --trace)

Here is what the trace outputs:

** Invoke assets:precompile:all (first_time) ** Execute assets:precompile:all ** Invoke assets:precompile:primary (first_time) ** Invoke assets:environment (first_time) ** Execute assets:environment ** Invoke environment (first_time) ** Execute environment ** Invoke tmp:cache:clear (first_time) ** Execute tmp:cache:clear ** Execute assets:precompile:primary rake aborted! couldn’t find file ‘jquery’ (in /home/vmcilwain/apps/test_app/app/assets/javascripts/application.js:13) /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/sprockets-2.1.3/lib/sprockets/context.rb:100:in resolve' /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/sprockets-2.1.3/lib/sprockets/context.rb:140:in require_asset’ /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/sprockets-2.1.3/lib/sprockets/directive_processor.rb:215:in process_require_directive' /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/sprockets-2.1.3/lib/sprockets/directive_processor.rb:165:in block in process_directives’ /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/sprockets-2.1.3/lib/sprockets/directive_processor.rb:163:in each' /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/sprockets-2.1.3/lib/sprockets/directive_processor.rb:163:in process_directives’ /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/sprockets-2.1.3/lib/sprockets/directive_processor.rb:97:in evaluate' /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/tilt-1.3.3/lib/tilt/template.rb:76:in render’ /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/sprockets-2.1.3/lib/sprockets/context.rb:177:in block in evaluate' /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/sprockets-2.1.3/lib/sprockets/context.rb:174:in each’ /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/sprockets-2.1.3/lib/sprockets/context.rb:174:in evaluate' /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/sprockets-2.1.3/lib/sprockets/processed_asset.rb:12:in initialize’ /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/sprockets-2.1.3/lib/sprockets/base.rb:241:in new' /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/sprockets-2.1.3/lib/sprockets/base.rb:241:in block in build_asset’ /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/sprockets-2.1.3/lib/sprockets/base.rb:262:in circular_call_protection' /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/sprockets-2.1.3/lib/sprockets/base.rb:240:in build_asset’ /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/sprockets-2.1.3/lib/sprockets/index.rb:89:in block in build_asset' /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/sprockets-2.1.3/lib/sprockets/caching.rb:19:in cache_asset’ /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/sprockets-2.1.3/lib/sprockets/index.rb:88:in build_asset' /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/sprockets-2.1.3/lib/sprockets/base.rb:163:in find_asset’ /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/sprockets-2.1.3/lib/sprockets/index.rb:56:in find_asset' /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/sprockets-2.1.3/lib/sprockets/bundled_asset.rb:16:in initialize’ /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/sprockets-2.1.3/lib/sprockets/base.rb:244:in new' /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/sprockets-2.1.3/lib/sprockets/base.rb:244:in build_asset’ /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/sprockets-2.1.3/lib/sprockets/index.rb:89:in block in build_asset' /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/sprockets-2.1.3/lib/sprockets/caching.rb:19:in cache_asset’ /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/sprockets-2.1.3/lib/sprockets/index.rb:88:in build_asset' /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/sprockets-2.1.3/lib/sprockets/base.rb:163:in find_asset’ /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/sprockets-2.1.3/lib/sprockets/index.rb:56:in find_asset' /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.3/lib/sprockets/static_compiler.rb:20:in block in compile’ /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/sprockets-2.1.3/lib/sprockets/base.rb:212:in block in each_logical_path' /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/sprockets-2.1.3/lib/sprockets/base.rb:200:in block (2 levels) in each_file’ /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/sprockets-2.1.3/lib/sprockets/base.rb:190:in each' /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/sprockets-2.1.3/lib/sprockets/base.rb:190:in each_entry’ /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/sprockets-2.1.3/lib/sprockets/base.rb:198:in block in each_file' /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/sprockets-2.1.3/lib/sprockets/base.rb:197:in each’ /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/sprockets-2.1.3/lib/sprockets/base.rb:197:in each_file' /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/sprockets-2.1.3/lib/sprockets/base.rb:210:in each_logical_path’ /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.3/lib/sprockets/static_compiler.rb:18:in compile' /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.3/lib/sprockets/assets.rake:56:in internal_precompile’ /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.3/lib/sprockets/assets.rake:70:in block (3 levels) in <top (required)>' /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:205:in call’ /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:205:in block in execute' /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:200:in each’ /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:200:in execute' /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:158:in block in invoke_with_call_chain’ /home/vmcilwain/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/monitor.rb:211:in mon_synchronize' /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:151:in invoke_with_call_chain’ /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:144:in invoke' /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.3/lib/sprockets/assets.rake:60:in block (3 levels) in <top (required)>’ /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:205:in call' /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:205:in block in execute’ /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:200:in each' /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:200:in execute’ /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:158:in block in invoke_with_call_chain' /home/vmcilwain/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/monitor.rb:211:in mon_synchronize’ /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:151:in invoke_with_call_chain' /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:144:in invoke’ /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:116:in invoke_task' /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in block (2 levels) in top_level’ /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in each' /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in block in top_level’ /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in standard_exception_handling' /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:88:in top_level’ /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:66:in block in run' /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in standard_exception_handling’ /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:63:in run' /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/bin/rake:33:in <top (required)>’ /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194@global/bin/rake:19:in load' /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194@global/bin/rake:19:in ’ /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/bin/ruby_noexec_wrapper:14:in eval' /home/vmcilwain/.rvm/gems/ruby-1.9.3-p194/bin/ruby_noexec_wrapper:14:in ’ Tasks: TOP => assets:precompile:primary

[snip]

Val,

The error you are getting means that, when running the rake task the jquery gem is not available. There are a few ways this can happen: Did you add the jquery gem only in the development group? In the asset group? Did you remember to run bundle install after adding the gem? What bundler groups are available in which environments (look into your application.rb file).

If answering the above questions doesn’t lead you to find the problem, post back your complete Gemfile and we will go from there.

Val,

Your Gemfile looks good to me and so does your application.js.

Are you using bundle exec rake to precompile? Can you post the error message you get about jquery?

Thanks for the response Ylan. I just now tried bundle exec rake assets:precompile:all and I am getting the same error about jquery. The error I am getting is:

rake aborted! couldn’t find file ‘jquery’ (in /home/vmcilwain/apps/test_app/app/assets/javascripts/application.js:13)

Tasks: TOP => assets:precompile:primary (See full trace by running task with --trace)

[snip]

Val,

The error you are getting means that, when running the rake task the jquery gem is not available. There are a few ways this can happen: Did you add the jquery gem only in the development group? In the asset group?

jquery rails is in the assets group of my gemfile

Did you remember to run bundle install after adding the gem?

Yes I did run bundle install after adding the gym.

What bundler groups are available in which environments (look into your application.rb file).

I am not sure I am looking in the right place in my application.rb file but here is what I saw at the top of the file:

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

If answering the above questions doesn’t lead you to find the problem, post back your complete Gemfile and we will go from there.

Here is what my gemfile looks like:

source ‘https://rubygems.org

gem ‘rails’, ‘3.2.3’

Bundle edge Rails instead:

gem ‘rails’, :git => ‘git://github.com/rails/rails.git’

gem ‘mysql2’ gem “meta_search”, “~> 1.1.1” gem “will_paginate”, “~> 3.0.0” gem “daemons”, “~> 1.1.3” gem ‘declarative_authorization’, “~> 0.5.1” gem ‘authlogic’, ‘3.1.0’, :git => ‘git://github.com/binarylogic/authlogic.git’ gem ‘paperclip’, ‘3.0.4’, :git => ‘git://github.com/thoughtbot/paperclip.git’ gem ‘tiny_mce’, ‘~> 0.1.7’, :git => ‘git://github.com/kete/tiny_mce.git’ gem ‘friendly_id’, ‘~> 4.0.6’, :git => ‘git://github.com/norman/friendly_id.git’ gem ‘simple-private-messages’, ‘0.0.0’, :git => ‘git://github.com/jongilbraith/simple-private-messages.git’

group :assets do gem ‘sass-rails’, ‘~> 3.2.3’ gem ‘coffee-rails’, ‘~> 3.2.1’ gem ‘jquery-rails’, ‘~> 2.0.1’

See GitHub - sstephenson/execjs: Run JavaScript code from Ruby for more supported runtimes

gem ‘therubyracer’, :platform => :ruby gem ‘uglifier’, ‘>= 1.0.3’ end

group :development do

gem ‘ruby-debug19’, ‘~> 0.11.6’, :require => ‘ruby-debug’

gem ‘ruby-debug19’, :require => ‘ruby-debug’

gem ‘thin’, ‘~> 1.2.11’ gem ‘exception_notification’, :git => ‘git://github.com/rails/exception_notification.git’ end

group :test do gem ‘factory_girl_rails’, “~> 1.0.1” gem ‘faker’, “~> 0.9.5” gem ‘shoulda’, “~> 2.11.3” gem ‘simplecov’, “~> 0.4.2” gem ‘mocha’, “~> 0.9.12” gem ‘turn’, :require => false gem ‘minitest’

=> below for spork

gem ‘guard’ gem “rb-fsevent” gem “spork” gem ‘spork-testunit’ gem “guard-spork” end

To use ActiveModel has_secure_password

gem ‘bcrypt-ruby’, ‘~> 3.0.0’

To use Jbuilder templates for JSON

gem ‘jbuilder’

Use unicorn as the app server

gem ‘unicorn’

Deploy with Capistrano

gem ‘capistrano’

[snip]

I don't see any obvious errors there, and I am assuming that you already checked line 13 of application.js for correctness. At this point, I am out of ideas on where the problem is. If I where you, I would start a blank rails application in which you *can* precompile the assets. Then starts looking for potential differences between the brand new app and yours.

Sorry, I can't be of more help.

The error you are getting means that, when running the rake task the jquery gem is not available. There are a few ways this can happen: Did you add the jquery gem only in the development group? In the asset group?

jquery rails is in the assets group of my gemfile

Did you remember to run bundle install after adding the gem?

Yes I did run bundle install after adding the gym.

What bundler groups are available in which environments (look into your application.rb file).

I am not sure I am looking in the right place in my application.rb file but here is what I saw at the top of the file:

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

If answering the above questions doesn’t lead you to find the problem, post back your complete Gemfile and we will go from there.

Here is what my gemfile looks like:

[snip]

I don’t see any obvious errors there, and I am assuming that you already checked line 13 of application.js for correctness. At this point, I am out of ideas on where the problem is. If I where you, I would start a blank rails application in which you can precompile the assets. Then starts looking for potential differences between the brand new app and yours.

So I did end up resolving this issue. There were 2 issues.

  1. jquery-rails doesn’t like being in the assets block of my Gemfile. Once I took it out of that block I was able to compile in production without any error. I happened to be passing through railscast and saw that one of Ryan Bates examples had that particular line under the assets block.

  2. I had an error in one of my .js files so removed the file and everything seems to be working as expected. I will see if I can figure out what is going on with that .js file at a later date.

Thanks for the help in trying to get me to solve this issue.