如何从引擎覆盖Rails应用路由? [英] How to override Rails app routes from an engine?

查看:83
本文介绍了如何从引擎覆盖Rails应用路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Rails应用程序,正在尝试将Rails引擎集成到其中。

I have a Rails app that I am trying to integrate a Rails engine in to.

主机应用程序可以捕获所有路线:

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"}

到目前为止,一切都达到了 / engine / envs 路线被应用程序捕获的所有路线所捕获。但是,我发现干扰路径是在引擎之后加载的,我不认为这些被捕获。有什么方法可以覆盖应用程序路由?

So far, everything is hitting the /engine/envs routes are getting caught by the application catch all routes. 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

这篇关于如何从引擎覆盖Rails应用路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆