【问题标题】:Remove Routes Specified in a Gem?删除 Gem 中指定的路线?
【发布时间】:2011-05-05 02:13:17
【问题描述】:

有没有办法删除 Rails 3 中 gem 中指定的路由?异常记录器 gem 指定了我不想要的路由。我需要像这样在路线上指定约束:

scope :constraints => {:subdomain => 'secure',  :protocol => 'https'} do 
    collection do
        post :query
        post :destroy_all
        get :feed
    end
end

基于Rails Engine docs,我想我可以创建一个猴子补丁并添加一个没有指定路由的路由文件到paths["config/routes"].paths 数组,但是该文件没有被添加到ExceptionLogger ::Engine.paths["config/routes"].paths

File: config/initializers/exception_logger_hacks.rb

ExceptionLogger::Engine.paths["config/routes"].paths.unshift(File.expand_path(File.join(File.dirname(__FILE__), "exception_logger_routes.rb")))

我在这里离基地很远吗?也许有更好的方法来做到这一点?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 rubygems routes overriding


    【解决方案1】:

    可以阻止 Rails 加载特定 gem 的路由,这样不会添加任何 gem 路由,因此您必须手动添加所需的路由:

    像这样在 application.rb 中添加一个初始化器:

    class Application < Rails::Application
    ...
    initializer "myinitializer", :after => "add_routing_paths" do |app|
      app.routes_reloader.paths.delete_if{ |path| path.include?("NAME_OF_GEM_GOES_HERE") }
    end
    

    【讨论】:

    • 我喜欢这个优雅的解决方案。与 the_role 管理页面配合使用。
    • 我将来可能也会使用这种方法,而不是我上面描述的方法。谢谢!
    • 我得到一个 RuntimeError: can't modify frozen Array when I try and do this :(
    • 这在 rails4 中效果很好,但在 rails5 中效果不一样 :(
    【解决方案2】:

    这是对我有用的一种方法。

    它不会“删除”路由,而是让您控制它们的匹配位置。您可能希望请求的每条路由都匹配某些内容,即使它是底部的全部 404。

    您的应用程序路由 (MyApp/config/routes.rb) 将首先加载(除非您修改了默认加载过程)。首先匹配的路线将优先。

    因此,您可以重新定义要显式阻止的路由,或者在 YourApp/config/routes.rb 文件的底部使用 catch all 路由来阻止它们。

    不幸的是,命名路由似乎遵循 ruby​​ 的“最后定义获胜”规则。因此,如果路由已命名并且您的应用程序或引擎使用这些名称,则您需要先定义路由(因此您的首先匹配)和最后(因此命名的路由指向您的意图,而不是引擎定义的。)

    要在引擎添加路由后重新定义引擎的路由,请创建一个名为

    的文件
    # config/named_routes_overrides.rb
    Rails.application.routes.draw do
      # put your named routes here, which you also included in config/routes.rb
    end
    
    # config/application.rb
    class Application < Rails::Application
      # ...
    
      initializer 'add named route overrides' do |app|
        app.routes_reloader.paths << File.expand_path('../named_routes_overrides.rb',__FILE__) 
        # this seems to cause these extra routes to be loaded last, so they will define named routes last.
      end
    end
    

    您可以在控制台中测试这个路由三明治:

    > Rails.application.routes.url_helpers.my_named_route_path
    => # before your fix, this will be the engine's named route, since it was defined last.
    
    > Rails.application.routes.recognize_path("/route/you/want/to/stop/gem/from/controlling")
    => # before your fix, this will route to the controller and method you defined, rather than what the engine defined, because your route comes first.
    

    修复后,这些调用应该相互匹配。

    (我最初在炼油厂宝石谷歌小组上发布了这个:https://groups.google.com/forum/?fromgroups#!topic/refinery-cms/N5F-Insm9co

    【讨论】:

    • 感谢您的回复。我认为现在,最简单的 hack 方法是执行以下操作: match "/logged_exceptions" => redirect("/404.html") match "/logged_exceptions/*other" => redirect(" /404.html") 丢弃了 gem 路由,新路由的范围为secure.mydomain.com/secure/logged_exceptions。
    猜你喜欢
    • 2017-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多