mc9
mc9

Reputation: 6341

Accessing engine routes from Rails.application.routes.url_helpers

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

  1. 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.

  2. 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

Answers (2)

Mat
Mat

Reputation: 2184

Assuming that your engine namespace is EngineName, it should be:

EngineName::Engine.routes.url_helpers.some_engine_path

Upvotes: 5

Maroo-b
Maroo-b

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

Related Questions