I need to know, from inside of a layout of a mountable engine, what path it\'s currently being mounted on. What would be the way to do it?
E.g. my routes.rb contains the
Evaluating the routes to get the mount path can give unexpected results when engines are mounted inside other engines.
When you take a look at https://github.com/rails/rails/pull/5008 you can read the comment by Jose Valim:
Also, an engine can be mounted inside another engine, so it is unlikely that this will give you the proper result. The best option is probably to have a configuration option where the developer will set the path.
For consistent results, I'm now using an accessor on the engine.
For example:
# APP/initializers/backend_core_engine.rb
BackendCore::Engine.mount_path = "/backend"
# APP/config/routes.rb
mount BackendCore::Engine => BackendCore::Engine.mount_path
# ENGINE/backend_core/lib/engine.rb
module BackendCore
class Engine < ::Rails::Engine
cattr_accessor :mount_path
end
end