简体   繁体   English

如何从引擎覆盖 Rails 应用程序路由?

[英]How to override Rails app routes from an engine?

I have a Rails app that I am trying to integrate a Rails engine in to.我有一个 Rails 应用程序,我正在尝试将 Rails 引擎集成到其中。

The host app has some catch all routes:主机应用程序有一些捕获所有路线:

  # magic urls
  match '/' => 'admin/rendering#show'
  match '*path/edit' => 'admin/rendering#show', :defaults => { :editing => true }
  match '*path' => 'admin/rendering#show'

It looks like the engine routes are loaded after the application catches all routes.看起来引擎路由是在应用程序捕获所有路由后加载的。

/sitemap.xml(.:format)                                            {:format=>"xml", :controller=>"admin/sitemaps", :action=>"show"}
                              /(.:format)                                                       {:controller=>"admin/rendering", :action=>"show"}
                              /*path/edit(.:format)                                             {:controller=>"admin/rendering", :action=>"show"}
                              /*path                                                            {:controller=>"admin/rendering", :action=>"show"}
           engine_envs GET    /engine/envs/:id(.:format)                                       {:controller=>"engine/envs", :action=>"show"}
                       PUT    /engine/envs/:id(.:format)                                       {:controller=>"engine/envs", :action=>"update"}
                jammit        /assets/:package.:extension(.:format)                             {:extension=>/.+/, :controller=>"jammit", :action=>"package"}

So far, everything is hitting the /engine/envs routes are getting caught by the application catch all routes.到目前为止,一切都在命中/engine/envs路由被应用程序捕获所有路由。 However I see that the jammit route is loaded after the engine and I don't believe those are getting caught.但是我看到堵塞路线是在引擎之后加载的,我不相信那些被抓住了。 Any way to override the app routes?有什么方法可以覆盖应用程序路由?

You could stick your engine routes in a method and then call that in your host app.您可以将引擎路由粘贴在一个方法中,然后在您的主机应用程序中调用它。

# engine routes.rb
module ActionDispatch::Routing
  class Mapper
    def engine_routes
      engine_envs GET    /engine/envs/:id(.:format)
      # ...
    end 
# ...

and then in your host app add the method before the catch-all route然后在您的主机应用程序中添加方法之前的包罗万象的路线

# host app routes.rb
MyTestApp::Application.routes.draw do
  # ... 

  engine_routes

  match '/' => 'admin/rendering#show'
  match '*path/edit' => 'admin/rendering#show', :defaults => { :editing => true }
  match '*path' => 'admin/rendering#show'
end

Routes are used in the order they are defined.路由按照定义的顺序使用。 The first routes to be read are the one of the host application, then of your engine.要读取的第一条路线是主机应用程序的路线,然后是引擎的路线。

As soon as a matching route is found, the search for a route is stopped.一旦找到匹配的路线,就停止搜索路线。

As far as I know, there are no way (I may be wrong about this) to override this feature other than to change your "mag据我所知,除了更改您的“mag

UPDATE: So that means that the order you see them in "rake routes" is the order they are processed.更新:这意味着您在“rake routes”中看到它们的顺序是它们的处理顺序。 As soon as a matching route is found, there you go.一旦找到匹配的路线,就会出现 go。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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