覆盖Rails 3 Engines提供的命名路由 [英] Overriding named routes provided by Rails 3 Engines

查看:51
本文介绍了覆盖Rails 3 Engines提供的命名路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究使用Rails引擎的Ruby on Rails 3(.0)应用程序。但是,在我的本地应用程序中,我想覆盖Rails引擎提供的路由之一。

I am working on a Ruby on Rails 3(.0) application that uses a Rails engine. However, in my local application, I want to override one of the routes provided by the Rails engine.

从引擎config / routes.rb:

From the engine config/routes.rb:

match 'their_named_route' => 'controller#action', :as => 'the_route'

从我的应用程序config / routes.rb中:

From my application config/routes.rb:

match 'my_named_route' => 'controller#action', :as => 'the_route'

但是,当我检查路线时,它们似乎都处于活动状态(并且它们的路线出现了至少在引擎控制器内才能赢得胜利)

However, when I inspect the routes, both seem to be active (and their route appears to "win", at least within the engine controllers)

$ rake routes
the_route  /my_named_route(.:format)    {:controller=>"controller", :action=>"action"}
the_route  /their_named_route(.:format) {:controller=>"controller", :action=>"action"}

是否有一种很好的方法来强制本地应用程序的命名路由优先?

Is there a good way to force my local application's named route to take priority?

推荐答案

恐怕没有这么简单的方法。路由在 lib / action_dispatch / routing / mapper.rb:271 中定义,该路由在RouteSet上调用add_route(在 rack-mount-0.6中定义.14 / lib / rack / mount / route_set.rb ,并在第71行附加名称)。没有remove_route方法,并且引擎的路由是最后添加的。您可以在使用Rails.application.routes.draw初始化应用程序之后手动添加路由,而不是将其包含在route.rb中,或者可以修补引擎。

I'm afraid that there's no such easy way. The routes are defined in lib/action_dispatch/routing/mapper.rb:271, which calls add_route on the RouteSet (defined in rack-mount-0.6.14/lib/rack/mount/route_set.rb, and on line 71 the name is attached). There's no remove_route method, and the Engine's route is added last. You can add your route manually after the application is initialized with Rails.application.routes.draw instead of having it in routes.rb, or you can patch the Engine.

这篇关于覆盖Rails 3 Engines提供的命名路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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