2
votes

I have a Rails app with Ember on the front-end. Today, I upgraded to Rails 4 and the only issue I cannot resolve is that my Handlebars templates are not being compiled or included as an asset (in development mode). I'm not getting any error messages.

I generated a brand new Rails 4 test app and installed Ember and it serves the Handlebars files just fine. All gems in the test app are present in my app. I tried running rake rails:upgrade, and that had no effect. I tried modifying the ember-rails source to output log messages and the messages and execution paths were the same for the test app and my app.

I'm requiring the templates the same way I always did:

require_tree ./templates

I tried using ember-rails 0.11.1 and from Github master, with no luck.

Related gem versions:

  * ember-data-source (0.0.5)
  * ember-rails (0.11.1 4dc902b)
  * ember-source (1.0.0.rc2.0)
  * sprockets (2.9.0)
  * sprockets-rails (2.0.0.rc3)
  * barber (0.4.1)
  • Templates are all in app/assets/javascripts/templates and all use the extension .handlebars. Although I tried .hbs and .js.hjs too.

From the Chrome console:

Ember.TEMPLATES Object {}

I am able to manually compile assets from the Rails command line like this:

environment = Sprockets::Environment.new
environment.append_path 'app/assets/javascripts'
a = environment['templates/test.handlebars']
a.to_s => "..compiled template.."

And the engine is registered:

environment.engines
=> {..".handlebars"=>Ember::Handlebars::Template..}

Rails.application.config.assets.paths includes the app/assets/javascripts folder, as it should.

Any help with this would be greatly appreciated, Thanks!

1

1 Answers

4
votes

I've narrowed it down to the following block in ember-rail's engine.rb file. For my app, it is sending register_engine on Sprockets, not app.assets. On the fresh Rails 4 test app, it was also doing this. However, in my app when I force the execution to use app.assets, the handlebar templates are compiled.

I believe since my app was upgraded from Rails 3, somewhere some configuration is missing to properly use Sprockets. I'll post more when I know.

 initializer "ember_rails.setup", :after => :append_assets_path, :group => :all do |app|
    sprockets = if ::Rails::VERSION::MAJOR == 4
      Sprockets.respond_to?('register_engine') ? Sprockets : app.assets
    else
      app.assets
    end

    sprockets.register_engine '.handlebars', Ember::Handlebars::Template
    sprockets.register_engine '.hbs', Ember::Handlebars::Template
    sprockets.register_engine '.hjs', Ember::Handlebars::Template
  end

UPDATE: For me,changing the railties order in application.rb was enough to fix the problem. I believe some other engine was clearing the Sprockets config, so by loading the Ember::Rails engine later in the process, I was able to hack a way around that issue.

config.railties_order = [:main_app, :all, Ember::Rails::Engine]