删除Rails引擎的路由(Removing Rails Engine's Routes)

编程入门 行业动态 更新时间:2024-10-28 03:24:55
删除Rails引擎的路由(Removing Rails Engine's Routes)

我已经阅读了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") end

In 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 end

If 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

更多推荐

本文发布于:2023-08-07 21:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466047.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   引擎   Rails   Routes   Engine

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!