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 ?


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' %>
链接地址: http://www.djcxy.com/p/96330.html

上一篇: 在CodeIgniter中为(:any)

下一篇: Rails不加载可挂载引擎的js