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

    where engine_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 use Rails.application.routes.url_helpers

    because there are many factory objects like this and they all inherit from the superclass that breaks url_helper

    into Rails.application.routes

    .

Any suggestions? Let me know if any clarification is needed.

+3


source to share


1 answer


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

      

0


source







All Articles