简体   繁体   中英

Removing Rails Engine's Routes

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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