Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

javascript_include_tag throwing nil object error on first load after deploy when used with cache in production

#1553

When I deploy my application, the first POST request causes the javascript_include_tag to fail with a nil object error for expand_sources. Additional requests do not trigger the error.


On line #5 of app/views/layouts/application.html.erb
2: <html xmlns="http://www.w3.org/1999/xhtml">
3: <title>SNPlotyper</title>
4: <%= stylesheet_link_tag :all, :cache => true %>
5: <%= javascript_include_tag :all, :cache => true %>
6: <script type="text/javascript" charset="utf-8">
7: var $jq = jQuery.noConflict();
8: </script>

gems/actionpack-2.2.2/lib/action_view/helpers/asset_tag_helper.rb:744:in `tag_sources'
gems/actionpack-2.2.2/lib/action_view/helpers/asset_tag_helper.rb:748:in `joined_contents'
gems/actionpack-2.2.2/lib/action_view/helpers/asset_tag_helper.rb:717:in `write_asset_file_contents'
gems/actionpack-2.2.2/lib/action_view/helpers/asset_tag_helper.rb:717:in `open'
gems/actionpack-2.2.2/lib/action_view/helpers/asset_tag_helper.rb:717:in `write_asset_file_contents'
gems/actionpack-2.2.2/lib/action_view/helpers/asset_tag_helper.rb:253:in `javascript_include_tag'
app/views/layouts/application.html.erb:5

Reported by MCW BBC · December 10th, 2008 @ 09:27 PM

State: stale
Milestone: 2.x
Assigned to: Frederick Cheung Frederick Cheung
Importance: none

Activity

  1. Frederick Cheung
    Frederick Cheung
    • Tag changed from 2.2.2, asset_tag_helper, expand_sources, javascript_include_tag to 2.2.2, asset_tag_helper, expand_sources, javascript_include_tag, patch

    I think this should fix it.

    December 11th, 2008 @ 12:07 AM

  2. jgeiger
    jgeiger
    • Tag changed from 2.2.2, asset_tag_helper, expand_sources, javascript_include_tag, patch to 2.2.2, assets_dir, asset_tag_helper, patch, rails.public_path

    This doesn't fix the issue, but it did lead me to do some more testing.

    If I run it on my dev box in production mode, the first GET request works properly, and will create /public/javascripts/all.js and /public/stylesheets/all.js

    The first POST request will fail with the following error:

    
    Errno::ENOENT in Analyses#visualize
    Showing app/views/layouts/application.html.erb where line #4 raised:
    No such file or directory - ./public/stylesheets/standard.css
    
    /opt/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/helpers/asset_tag_helper.rb:561:in `read'
    /opt/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/helpers/asset_tag_helper.rb:561:in `contents'
    /opt/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/helpers/asset_tag_helper.rb:748:in `joined_contents'
    /opt/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/helpers/asset_tag_helper.rb:748:in `collect'
    /opt/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/helpers/asset_tag_helper.rb:748:in `joined_contents'
    /opt/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/helpers/asset_tag_helper.rb:717:in `write_asset_file_contents'
    /opt/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/helpers/asset_tag_helper.rb:717:in `open'
    /opt/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/helpers/asset_tag_helper.rb:717:in `write_asset_file_contents'
    /opt/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/helpers/asset_tag_helper.rb:393:in `stylesheet_link_tag'
    /Users/me/workspace/webapp/app/views/layouts/application.html.erb:4
    

    This will also now create the new file:
    /public/images/public/stylesheets/all.css

    Run the same POST request again, and it creates:
    /public/images/public/javascripts/all.js

    It also throws this error/trace:

    
    Errno::ENOENT in Analyses#visualize
    Showing app/views/layouts/application.html.erb where line #5 raised:
    No such file or directory - ./public/javascripts/prototype.js
    
    /opt/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/helpers/asset_tag_helper.rb:561:in `read'
    /opt/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/helpers/asset_tag_helper.rb:561:in `contents'
    /opt/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/helpers/asset_tag_helper.rb:748:in `joined_contents'
    /opt/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/helpers/asset_tag_helper.rb:748:in `collect'
    /opt/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/helpers/asset_tag_helper.rb:748:in `joined_contents'
    /opt/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/helpers/asset_tag_helper.rb:717:in `write_asset_file_contents'
    /opt/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/helpers/asset_tag_helper.rb:717:in `open'
    /opt/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/helpers/asset_tag_helper.rb:717:in `write_asset_file_contents'
    /opt/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/helpers/asset_tag_helper.rb:253:in `javascript_include_tag'
    /Users/me/workspace/webapp/app/views/layouts/application.html.erb:5
    

    The third POST request is then successful.

    It seems that somewhere along the way ASSETS_DIR or Rails.public_path are getting messed up and it's redefined on the POST requests, which cause the cached stylesheets and javascripts to be placed in a weird area.

    line 104 of asset_tag_helper.rb

    
    ASSETS_DIR      = defined?(Rails.public_path) ? Rails.public_path : "public"
    

    December 11th, 2008 @ 02:59 PM

  3. Frederick Cheung
    Frederick Cheung

    So what i did was change a uniq! to a uniq (because the return value of uniq! is nil if nothing as removed).

    A separate issue it would seem is that :all tries to collect all the js that rails assume are there (ie prototype, scriptaculous etc...) and which I'm guessing you;re not using and may even have removed

    December 11th, 2008 @ 03:25 PM

  4. jgeiger
    jgeiger

    I'm using all of the default js files, and some others as well. What my further research showed above is that there is something wrong with where the cached files are being created on my POST request. It looks like the ASSETS_DIR is being set to public from inside the images directory, which is odd. It's like Rails.public_path is being reset in some way.

    December 11th, 2008 @ 05:54 PM

  5. Pratik
    Pratik
    • Assigned user set to Frederick Cheung

    December 22nd, 2008 @ 03:49 AM

  6. Frederick Cheung
    Frederick Cheung

    Can you condense this into a small example (ie actual code)?

    December 23rd, 2008 @ 12:45 AM

  7. Frederick Cheung
    Frederick Cheung

    A lot of the asset tag stuff was changed in 104898f. Has that changed anything for you?

    January 4th, 2009 @ 06:36 PM

  8. jgeiger
    jgeiger

    I'm not using edge, but I would have to believe this will fix it.

    This line that was removed is very telling to me since my all.js and all.css ended up getting stuck into the images folder after requests.

    • module ImageAsset
    • DIRECTORY = 'images'.freeze

    January 5th, 2009 @ 02:28 PM

  9. Pratik
    Pratik
    • State changed from new to stale

    Putting on "stale". Please reopen if it's still an issue.

    Thanks.

    March 10th, 2009 @ 11:07 AM