leshill / handlebars_assets

Use handlebars.js templates with the Rails asset pipeline.
MIT License
649 stars 159 forks source link

"undefined method `register_engine' for nil" error when using `sprockets-rails` 3 #145

Closed tjgrathwell closed 8 years ago

tjgrathwell commented 8 years ago

sprockets-rails 3 was just released, and apparently app.assets is now nil at initializer-time, so app.assets.register_engine won't work.

One possible solution is to use config.assets.configure do |env| instead, which seems to be the more 'proper' way:

diff --git a/lib/handlebars_assets/engine.rb b/lib/handlebars_assets/engine.rb
index 8a4be6e..c219840 100644
--- a/lib/handlebars_assets/engine.rb
+++ b/lib/handlebars_assets/engine.rb
@@ -2,8 +2,10 @@ module HandlebarsAssets
   # NOTE: must be an engine because we are including assets in the gem
   class Engine < ::Rails::Engine
     initializer "handlebars_assets.assets.register", :group => :all do |app|
-      ::HandlebarsAssets::register_extensions(app.assets)
-      app.assets.version += "#{::HandlebarsAssets::VERSION}"
+      config.assets.configure do |env|
+        ::HandlebarsAssets::register_extensions(env)
+        app.assets.version += "#{::HandlebarsAssets::VERSION}"
+      end
     end
   end
 end

Another solution is to use the new register_transformer code from https://github.com/leshill/handlebars_assets/pull/135 but relax the constraint on Sprockets::VERSION so it also applies to Sprockets 3. The register_transformer syntax should work in Sprockets 3 and up, and that code will be needed for Sprockets 4 anyway.

Related issue in the sprockets-rails repo: https://github.com/rails/sprockets-rails/issues/237

attenzione commented 8 years ago

have the same issue

zedtux commented 8 years ago

@tjgrathwell as I have this issue too, I tried your sprockets4 branch you did but it's still failing with the some error.

I have then checked the version of sprockets I'm using :

irb(main):001:0> Gem::Version.new(Sprockets::VERSION)
=> #<Gem::Version "3.5.2">

so it seems that your branch is not compatible with sprockets 3.x.

tjgrathwell commented 8 years ago

@zedtux I've added a second PR (#146) with the gem version requirement changed, so if you want you can test using

gem 'handlebars_assets', github: 'tjgrathwell/handlebars_assets', branch: 'sprockets-register-transformers'
AlexRiedler commented 8 years ago

@tjgrathwell I think master is now in a working state, I still want to do some manual testing over the next week. I have ran against sprockets 3.x and 2.x, it seems to work. I am just not 100% on the rails engine integration at this time.

tjgrathwell commented 8 years ago

@AlexRiedler Seems to work for me on all combinations of sprockets-rails 2/3 and sprockets 3/4. Hopefully still works against Sprockets 2.x if you're still interested in that. Good work!

zedtux commented 8 years ago

@tjgrathwell your branch sprockets-register-transformers works fine.

kostia commented 8 years ago

Do you already know, when a release with this fix will be available?

sblackstone commented 8 years ago

+1 for @tjgrathwell solution - working fine in both development and production environments..

tom-lord commented 8 years ago

+1 the fix looks good to me

As a temporary fix for your application, you can simply lock the sprockets-rails version in your Gemfile:

gem 'sprockets-rails', '~> 2.3'
kostia commented 8 years ago

gem 'sprockets-rails', '~> 2.3'

Not if you need sprockets >= 3…

tom-lord commented 8 years ago

@kostia Well no, obviously not. Hopefully a proper fix will be released soon - my suggestion is only a temporary workaround that should be good for the majority of applications.

gregblass commented 8 years ago

Perfect time to refactor to react.

tjgrathwell commented 8 years ago

@leshill Any word on a new release? The code on master has been working alright for me.

badlamer commented 8 years ago

+1

AlexRiedler commented 8 years ago

Release of v0.23.0 should fix all your wonderful problems :) Release Today!

tjgrathwell commented 8 years ago

Works for me, thanks! :cake:

tagliala commented 8 years ago

Thanks!

zedtux commented 8 years ago

Awesome, all is now working fine ! Thank you @AlexRiedler :+1: