我已经阅读了Rails 3 Engines提供的Overriding命名路由,并征求了一些新的答案,但如果您的目标是批量删除路由,那么所有解决方案都会非常糟糕。
预先覆盖路线以覆盖发动机的路线是有效的; 然而,当发动机资源丰富时,它变得耗时。 嵌套资源复杂化。
An::Engine.routes.prepend do match "/route_goes_here" => redirect("/404") end就我而言,我希望所有的路线都消失了。
I've read Overriding named routes provided by Rails 3 Engines and solicited some fresh answers, but all of the solutions feel pretty gross if you're goal is to remove routes in bulk.
Prepending routes to override the engine's routes was effective; however, it becomes time consuming when the engine is resource rich. Nesting resources compounds complexity.
An::Engine.routes.prepend do match "/route_goes_here" => redirect("/404") endIn my case, I want to feel confident all the routes are gone gone.
最满意答案
如果您要删除路由,那么您很可能也需要修改其他代码(想想使用路径帮助程序的代码,一旦路由被删除就不会定义)。
对于我的SuperDuper :: Engine,我创建了一个SuperDuper :: Configuration模块。 在引擎的config / routes.rb中,我根据配置定义了路由。 定义辅助方法以有条件地呈现各种链接/表单/按钮。
通过覆盖配置模块的设置,您可以控制在应用程序启动时如何或是否包含引擎的路由。
module SuperDuper module Configuration def self.documents=(bool) @documents = bool end def self.documents @documents.nil? ? true : @documents end end endIf you're going to remove routes, there's a strong chance you're going to need to modify other code as well (think of code that uses path helpers that won't be defined once the routes are removed).
For my SuperDuper::Engine, I created a SuperDuper::Configuration module. In the engine's config/routes.rb I define the routes based on the configuration. Define helper methods to render your various links/forms/buttons conditionally as well.
By overriding the configuration module's settings, you can control how or whether the engine's routes will be included when the application starts.
module SuperDuper module Configuration def self.documents=(bool) @documents = bool end def self.documents @documents.nil? ? true : @documents end end end更多推荐
发布评论