Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails not loading mountable engine's js

I have a mountable rails engine included in Gemfile as

gem 'my_engine', :path => 'engines/my_engine'

and mounting in main app as

Rails.application.routes.draw do
  mount MyEngine::Engine => "/blog", as: 'blog_engine'
end

In app/engines/my_engine/app/assets/javascripts/my_engine/application.js I have

alert('hello');

In app/engines/my_engine/lib/my_engine/engine.rb I added

module MyEngine
  class Engine < ::Rails::Engine
    isolate_namespace MyEngine

    # Append engine's migrations to root app's migrations
    initializer :append_migrations do |app|
      unless app.root.to_s.match root.to_s
        config.paths["db/migrate"].expanded.each do |expanded_path|
          app.config.paths["db/migrate"] << expanded_path
        end
      end
    end

    config.autoload_paths += Dir["#{config.root}/spec/support"]

    initializer "my_engine.precompile" do |app|
      app.config.assets.paths << Rails.root.join('/engines/my_engine/app/assets/javascripts')
      app.config.assets.precompile << "my_engine/application.js"
    end

  end
end

But when I reload http://localhost:3000/blog alert message is not showing up? What am I missing ?

like image 293
Tony Vincent Avatar asked May 12 '17 11:05

Tony Vincent


1 Answers

You also need to actually insert the JavaScript into your page. Do you have a javascript_include_tag pulling in the application.js file in your blog code? It often gets put into one of the layouts:

<%= javascript_include_tag 'my_engine/application' %>
like image 144
lobati Avatar answered Nov 08 '22 12:11

lobati