0

I have been having this pretty rough time with assets on production. It came down to me trying to override the sprokets helper module to try and see what was up... When I re-wrote it to the following:

module Sprockets
  module Rails
    module Helper
      def compute_asset_path(path, options = {})

It would not run. I tried it in my local environment, and it runs perfectly. Is there a reason the production environment would not allow my assets to render in digest form, but my local environment would? This is related to what this gent was asking...

Rails 4.0.3 generating incorrect asset paths with asset_sync

Here is the error I receive after trying to override that method (which doesn't have an error when running with RAILS_ENV=development):

ActionView::Template::Error (undefined local variable or method `digest_path' for #<#<Class:0x000001034a99e0>:0x000001034a81d0>):

Asset Gems in Gemfile for reference:

source 'http://rubygems.org'
# ruby '2.1.1'

gem 'rails', '4.0.4'
gem 'jbuilder', '~> 1.2'
gem 'devise'
gem 'devise_invitable'
gem 'figaro'
gem 'mysql2'
gem 'simple_form'
gem 'kaminari'
gem 'statistics'
gem 'possessive'
gem 'geocoder'
gem 'nokogiri'
gem 'asset_sync'
gem 'sprockets-rails', :require => 'sprockets/railtie'
gem 'ledermann-rails-settings', :require => 'rails-settings'
gem 'public_activity'

group :assets do
  gem 'therubyracer'
  gem 'sass-rails', '~> 4.0.0'
  gem 'uglifier', '>= 1.3.0'
  gem 'coffee-rails', '~> 4.0.0'
end

group :development do
  gem 'better_errors'
  gem 'binding_of_caller', :platforms=>[:mri_19, :mri_20, :rbx]
  gem 'guard-bundler'
  gem 'guard-rails'
  gem 'quiet_assets'
  gem 'rails_layout'
  gem 'rb-fchange', :require=>false
  gem 'rb-fsevent', :require=>false
  gem 'rb-inotify', :require=>false
end

group :test do
  gem 'email_spec', '>= 1.4.0'
  gem 'launchy', '>= 2.2.0'
  gem 'capybara', '>= 2.0.3'
  gem 'database_cleaner', '>= 1.0.0.RC1'
  gem 'cucumber-rails', '>= 1.3.1', :require => false
end

group :production do
  gem 'rails_12factor'
end

gem 'rspec-rails', '>= 2.12.2', :group => [:development, :test]
gem 'factory_girl_rails', '>= 4.2.0', :group => [:development, :test]
gem 'teaspoon', '>= 0.7.4', :group => [:development, :test]
gem 'cancan', '>= 1.6.9'
gem 'rolify', '>= 3.2.0'
gem 'stripe-rails'
gem 'faker'
gem 'open4'
gem 'unf'

When I run this in development mode, it runs completely fine. When I run this through production mode (even with the same config file) it does not inherit the View properties like digest_path or asset_digest_path is null or manifest, etc.

module Sprockets
  module Rails
    module Helper

      def compute_asset_path(path, options = {})
        if digest_path = asset_digest_path(path)
          path = digest_path if true # BUG: digest assets doesn't work on live, let's just bake it
          path += "?body=1" if options[:debug]
          File.join(assets_prefix || "/", path)
        else
          super
        end
      end

      def asset_digest_path(path, options = {})
        if manifest = assets_manifest
          if digest_path = manifest.assets[path]
            return digest_path
          end
        end

        if environment = assets_environment
          if asset = environment[path]
            return asset.digest_path
          end
        end
      end

    end
  end
end

module ActionView
  module Helpers
    module AssetUrlHelper
      def compute_asset_path(source, options = {})
        dir = ASSET_PUBLIC_DIRECTORIES[options[:type]] || ""
        File.join(dir, source)
      end
    end
  end
end
Community
  • 1
  • 1
Jonathan Reyes
  • 1,387
  • 1
  • 10
  • 23

1 Answers1

0

Hopefully this will help save my fellow programmer friends some head banging :D

I was uploading the files to S3, I didn't realize that the manifest wasn't loaded by Rails. You can have all your production settings right (like above and in other threads), but if you don't have the manifest.json file readable by Rails, it will still generate /javascript/* (example) urls.

I was still having trouble with multi_json gem's latest version, so I downgraded it to 1.7.8 and it works fine.

gem 'multi_json', '1.7.8'

That's so it can read the manifest.json file which rake assets:precompile creates.

There is a debate on this sprockets thread https://github.com/rails/sprockets-rails/issues/107 on whether your manifest file should be in git or just on a deploy script, do what suits you best, just make sure it is findable in:

/public/assets/manifest.json 

or specifiy it yourself with

config.assets.manifest = '...'

That may or may not be depricated.

Cheers!

Jonathan Reyes
  • 1,387
  • 1
  • 10
  • 23