Reputation: 6341
I would like to know how to access engine routes by using Rails.application.routes.url_helpers
.
I have a factory object that creates a string including a dynamically generated url. Currently, I can generate urls by using Rails.application.routes.url_helpers.(INSERT PATH NAME)
.
However, it can only access the routes in the main app. I cannot access the routes of engines mounted on the main app.
Things I have tried
I've tried to use Rails.application.routes.engine_name
where engine_name
is the name under which the engine is mounted on the main app.
I've tried to use MyEngine::Engine.routes.url_helpers
to access the routes in the Engine. It works, but I would like to use Rails.application.routes.url_helpers
because there are many factory objects like this one, and they are all inheriting from the superclass that delgates url_helper
to Rails.application.routes
.
Any suggestions? Let me know if any clarification is needed.
Upvotes: 10
Views: 3733
Reputation: 2184
Assuming that your engine namespace is EngineName
, it should be:
EngineName::Engine.routes.url_helpers.some_engine_path
Upvotes: 5
Reputation: 197
You have to use engine route proxy method. From your example call the url helper using the following syntax as example:
my_engine_engine.articles_path
To rename the proxy method helper, just edit the routes config file inside your rails application:
mount MyEngine::Engine => '/app', :as => 'my_engine'
so you can now call the previous example:
my_engine.articles_path
Upvotes: 3