Narayanan V
Narayanan V

Reputation: 31

how to get the routes of my app in which my engine is mounted from the engine?

I want to list all the routes present in application from an engine through code written in engine, like fetching routes of application in application by using:

routes = Rails.application.routes.routes
routes.collect {|r| r.path.spec.to_s }

the above command lists down the routes available in main application

1. What is the code to be used in engine to fetch the routes of application "in engine"?

I'm not able to fetch the routes if I use the above code in engine

Upvotes: 1

Views: 1169

Answers (1)

Vasfed
Vasfed

Reputation: 18474

In engine Rails.application.routes.routes will be routes of its parent application.

YourEngineClass::Engine.routes.routes will be routes of the engine (keep in mind that these will not have route prefix, that comes from main app)

Update: routes are available after app initialisation. For them to be available in rake, you need to make your task dependent on :environment:

namespace :your_engine do
  desc "test"
  task some_task: :environment do
    puts "Routes:"
    puts Rails.application.routes.routes.collect {|r| r.path.spec.to_s }.join("\n")
  end 
end

Upvotes: 1

Related Questions