Accessing engine routes from Rails.application.routes.url_helpers
I would like to know how to access engine routes using Rails.application.routes.url_helpers
.
I have a factory object that creates a string containing a dynamically generated url. Currently I can generate urls using Rails.application.routes.url_helpers.(INSERT PATH NAME)
.
However, it can only access routes in the main application. I cannot access the engine routes set in the main application.
Things i have tried
-
I tried to use
Rails.application.routes.engine_name
whereengine_name
is the name under which the engine is mounted in the main application. -
I tried to use
MyEngine::Engine.routes.url_helpers
to access routes in Engine. It works, but I would like to useRails.application.routes.url_helpers
because there are many factory objects like this and they all inherit from the superclass that breaksurl_helper
intoRails.application.routes
.
Any suggestions? Let me know if any clarification is needed.
source to share
You need to use the proxy engine method. In your example, call the URL helper using the following syntax as an example:
my_engine_engine.articles_path
To rename the proxy method helper, simply edit the routes config file inside your rails app:
mount MyEngine::Engine => '/app', :as => 'my_engine'
so you can now call the previous example:
my_engine.articles_path
source to share