我在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
时,警报信息没有显示出来?我错过了什么?
答案 0 :(得分:4)
您还需要将JavaScript实际插入页面。您的博客代码中的javascript_include_tag
文件是否有application.js
?它经常被放入其中一个布局中:
<%= javascript_include_tag 'my_engine/application' %>