22

I am experiencing a problem similar to this question: Rails asset pipeline not including required files in application.js manifest However, that question was closed, so I am re-asking it, but with the specifics of my situation.)

Environment:

Ruby 2.0.0, Rails 3.2.8, OSX 10.7.5, Chrome 28.0.1500.95

The main problem I am experiencing

Files placed in "/app/assets/javascripts" do not appear to be compiling to or appearing in "/public/assets"

An Example

If I place a file such as this test.js file in app/assets/javascripts:

  alert("Hello!");

and then reload a page in my app, this alert should appear. (I am replicating what Ryan Bates does in his Railscast on the Asset Pipeline at ~6:30 into the video) However, no alert appears.

Debugging attempts

Some tests I did to try to debug this.

  • (As recommended by @Aidan below) If I "rm -rf public/assets", then add "javascript_include_tag 'test'" to my layout, the alert does appear. However, maybe because I'm a n00b, I don't know how that helps me debug the problem.
  • If I add //= require_test to app/javascripts/application.js to my manifest file, this does not result in the alert appearing.

My manifest file (app/javascripts/application.js)

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
// removed /sitewide from require_tree
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require_tree .

My application.rb file

 # Enable the asset pipeline
 config.assets.enabled = true

Rails console output for "y Rails.application.config.assets.paths"

"/Users/Anders/Dropbox/dev/suggestion-box-app/app/assets/images",
 "/Users/Anders/Dropbox/dev/suggestion-box-app/app/assets/javascripts",
 "/Users/Anders/Dropbox/dev/suggestion-box-app/app/assets/stylesheets",
 "/Users/Anders/.rvm/gems/ruby-2.0.0-p195@suggestion-box-app/gems/coffee-rails-3.2.2/lib/assets/javascripts",
 "/Users/Anders/.rvm/gems/ruby-2.0.0-p195@suggestion-box-app/gems/jquery-rails-3.0.4/vendor/assets/javascripts"

My views/layouts/application.html.haml file

!!! 5
%html
  %head
    %meta{:charset => "utf-8"}/
    %meta{:content => "width=device-width", :name => "viewport"}/
    %meta{:content => "yes", :name => "apple-mobile-web-app-capable"}/
    %title My App
    %script{:type=>"text/javascript", :src=>"//use.typekit.net/wjb6ybj.js"}
    :javascript
      try{Typekit.load();}catch(e){}
    = stylesheet_link_tag "screen", :media => "all"
    = csrf_meta_tags
    = javascript_include_tag "application"
  %body
    #container
      = render "layouts/flashes"
      = yield
      = render "layouts/footer"

Source from an app page viewed in localhost:3000

<!DOCTYPE html>
<html>
  <head>
    <meta charset='utf-8'>
    <meta content='width=device-width' name='viewport'>
    <meta content='yes' name='apple-mobile-web-app-capable'>
    <title>My app</title>
    <script src='//use.typekit.net/wjb6ybj.js' type='text/javascript'></script>
    <script>
      try{Typekit.load();}catch(e){}
    </script>
    <link href="/assets/screen.css?body=1" media="all" rel="stylesheet" type="text/css" />
    <meta content="authenticity_token" name="csrf-param" />
    <meta content="FqMtH+rs6or9HRx+RL4bWpQyLTNPM8BKLrcf/xZknP8=" name="csrf-token" />
    <script src="/assets/application.js?body=1" type="text/javascript"></script>
  </head>
  <body>
    <div id='container'>
      ....
    </div>
  </body>
</html>

What I see when viewing "localhost:3000/assets/application.js"

    /*!
 * jQuery JavaScript Library v1.9.1
 * http://jquery.com/
 *
 * Includes Sizzle.js
 * http://sizzlejs.com/
 *
 * Copyright 2005, 2012 jQuery Foundation, Inc. and other contributors
 * Released under the MIT license
 * http://jquery.org/license
 *
 * Date: 2013-2-4
 */
(function(e,t){function P(e){var t=e.length,n=b.type(e);return b.isWindow(e)?!1:e.nodeType===1&&t?!0:n==="array"||n!=="function"&&(t===0||typeof t=="number"&&t>0&&t-1 in e)}function B(e){var t=H[e]={};return b.each(e.match(E)||[],function(e,n){t[n]=!0}),t}function I(e,n,r,i){if(!b.acceptData(e))return;var s,o,u=b.expando,a=typeof n=="string",f=e.nodeType,c=f?

[...remainder of jquery excluded]

Should I not see the test.js file here?

Any suggestions as to what the problem might be? Is there any additional info I can provide to help debug this issue? Thanks for any help!

6
  • 2
    Do you have a file in public/assets/application.js? Run rake assets:clean to make sure you don't.
    – deefour
    Aug 1, 2013 at 21:55
  • I tried this, but it did not have any impact.
    – andersr
    Aug 1, 2013 at 23:16
  • 2
    Try a few things, just to make sure it can find that file: 1) rm -rf public/assets, 2) remove jquery and jquery_ujs from application.js, and 3) add javascript_include_tag "test" to your layout. Aug 3, 2013 at 22:16
  • @AidanFeldman - thanks for this suggestion. I did all of the above, and upon page refresh, the "hello!" alert now appears(!)
    – andersr
    Aug 4, 2013 at 17:32
  • K, well that's good... now work your way backwards and see where the problem is! I imagine it was step #1. Aug 4, 2013 at 18:02

2 Answers 2

11

The problem appears to have been related to my rails/gem versions. After updating from Rails 3.2.8 to Rails 3.2.14 (which also required updating the rack gem from 1.4.1 to 1.4.5) the issue was resolved and items in assets/javascripts are now being compiled/added to public/assets. Note that I created a completely new gemset as part doing this, so the issue may have had nothing to do with the Rails version and simply was a questions of a "corrupted" gem set that needed to be re-created.

2

Rails will compile all the targets listed in config.assets.precompile. These targets will be compiled to public/assets/. That config item already includes "application.js" and other some targets, so you need to add any other targets you want to be precompiled to public/assets/. The source file to be compiled for any particular target are automatically computed based on the name of the target; e.g., a target of test.js would be compiled from app/assets/javascripts/test.js or from app/assets/javascripts/test.js.coffee, whichever exists.

8
  • 3
    Maybe I am asking the wrong question. I simply want JS files I place in assets/javascripts to be accessible in public/assets. My understanding is that this should be handled by including //= require_tree . in the manifest file. So I guess I'm not understanding your answer. Could you maybe explain it differently?
    – andersr
    Aug 1, 2013 at 23:16
  • 3
    @andersr you seem to understand clearly that test.js should be included in the application.js file from your require_tree . line. @yfeldblum's answer isn't really relevant here; you don't need to modify config.assets.precompile to get test.js included in your application.js file. What you should see in development is every .js file dependency included from your =require_tree . line added to your layout's HTML source as separate <script> tags. One of these tags should be for your test.js file.
    – deefour
    Aug 2, 2013 at 0:23
  • 2
    The above comment assumes you a) are in :development, b) have config.assets.debug = true, and c) are including your script tag in your layout with <%= javascript_include_tag "application" %>
    – deefour
    Aug 2, 2013 at 0:24
  • @Deefour thanks for your clarifications. Yes, I am in development and I have the javascript include tag in my view. I added the exact markup above for your reference. However, I do not have config.assets.debug = true. Would that go in /config/application.rb?
    – andersr
    Aug 2, 2013 at 13:07
  • That goes in config/environments/development.rb typically.
    – deefour
    Aug 2, 2013 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.