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' %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With