Rails没有加载可安装的引擎的js

时间:2017-05-12 11:37:28

标签: ruby-on-rails asset-pipeline rails-engines

我在Gemfile中包含了一个可安装的rails引擎作为

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

并在主应用中安装

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

app/engines/my_engine/app/assets/javascripts/my_engine/application.js我有

alert('hello');

app/engines/my_engine/lib/my_engine/engine.rb我添加了

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

但是当我重新加载http://localhost:3000/blog时,警报信息没有显示出来?我错过了什么?

1 个答案:

答案 0 :(得分:4)

您还需要将JavaScript实际插入页面。您的博客代码中的javascript_include_tag文件是否有application.js?它经常被放入其中一个布局中:

<%= javascript_include_tag 'my_engine/application' %>