6

I'm writing a mountable Rails 3 Engine, and I am combining my engine's routes with the host app's. However, the host app's routes are taking precedence over my engine's routes. Is there a way I can override the host app's routes (specifically the root route)?

Here's my engine's routes in my_enging/config/routes.rb:

MyEngine::Engine.routes.draw do
 root :to => "home#index"
end

# Mount engine routes to host application
Rails.application.routes.draw do
  mount MyEngine::Engine, :at => "/"
end

And here's the results of rake routes, with the host's root sitting at the top:

root  / welcome#index
my_engine  / MyEngine::Engine
root  / home#index

Spree is one gem that does this, but I haven't been able to find how they're implementation this.

dhulihan
  • 10,123
  • 8
  • 37
  • 45

1 Answers1

0

People are more likely to do an opposite thing, so I can't figure out why you need this, because ability to override engine routes is supposed to be expected. Actually I have no answer how to force engine to override application routes, but:

Spree is adding routes dynamically somehow via eval_block, I presume.

You can just skip root to: as well as all undesired routes in your application at all, and then Engine routes will emerge. Do not keep them both. I don't see root specified in your application, but I've just run an experiment and everything works!

W12::Engine.routes.draw do
  root to: 'details#index'
end

MyCarsApplication.routes.draw do
  mount Common::Engine => "/"
end

And there is only one route:

root / details#index

This post about creating not isolated engines also might help.

If you found the exact answer, please post it.

Community
  • 1
  • 1
Nick Roz
  • 3,002
  • 2
  • 30
  • 49