简体   繁体   中英

Override host app's root route from Mountable Engine

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM