sikachu / sprockets-redirect

A Rack middleware for Rails >= 3.1.0 with asset pipeline and asset digest enabled. This middleware is used to redirect any request to static asset without a digest to the version with digest in its filename by reading the assets manifest file generated after you run `rake assets:precompile`
http://rubygems.org/gems/sprockets-redirect
MIT License
72 stars 26 forks source link

undefined method `[]' for nil:NilClass with sprockets-rails 3.0.0 #12

Closed transparentech closed 8 years ago

transparentech commented 8 years ago

I updated my app to rails 4.2.5 which brought in sprockets-rails 3.0.0. Rails.application.assets is now nil by default in production. See this issue:

https://github.com/rails/sprockets-rails/issues/237

This causes sprockets-redirect to throw an "undefined method for nil:NilClass":

[2015-12-22 13:40:25] ERROR NoMethodError: undefined method `[]' for nil:NilClass
    /Users/nick/.gem/ruby/2.2.2/gems/sprockets-redirect-0.3.0/lib/sprockets/redirect.rb:64:in `asset_exists?'
    /Users/nick/.gem/ruby/2.2.2/gems/sprockets-redirect-0.3.0/lib/sprockets/redirect.rb:60:in `asset_path_matched?'
    /Users/nick/.gem/ruby/2.2.2/gems/sprockets-redirect-0.3.0/lib/sprockets/redirect.rb:55:in `should_redirect?'
    /Users/nick/.gem/ruby/2.2.2/gems/sprockets-redirect-0.3.0/lib/sprockets/redirect.rb:45:in `call'
    /Users/nick/.gem/ruby/2.2.2/gems/railties-4.2.5/lib/rails/engine.rb:518:in `call'
    /Users/nick/.gem/ruby/2.2.2/gems/railties-4.2.5/lib/rails/application.rb:165:in `call'
    /Users/nick/.gem/ruby/2.2.2/gems/rack-1.6.4/lib/rack/lock.rb:17:in `call'
    /Users/nick/.gem/ruby/2.2.2/gems/rack-1.6.4/lib/rack/content_length.rb:15:in `call'
    /Users/nick/.gem/ruby/2.2.2/gems/rack-1.6.4/lib/rack/handler/webrick.rb:88:in `service'
    /Users/nick/.rubies/ruby-2.2.2/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
    /Users/nick/.rubies/ruby-2.2.2/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
    /Users/nick/.rubies/ruby-2.2.2/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'

My workaround was to set config.assets.compile = true in production.rb, but I'm not sure that's the best or most efficient solution. Would be happy for any other workaround suggestions or official solutions.

sikachu commented 8 years ago

Thanks for bringing this issue to my attention. I can take a look later on this week. It looks like we need to fix something to make our gem compatible with sprocket-rails 3.0.0.

derekprior commented 8 years ago

See: https://github.com/rails/sprockets-rails/issues/306#issuecomment-169119377 for possible explanation.

sikachu commented 8 years ago

This was fixed! I'm going to release v1.0.0 very soon.

ericboehs commented 8 years ago

I just ran into this as well. Looking forward to the release.

transparentech commented 8 years ago

Update to version 1.0.0, removed my workaround and it works again. Thanks!