【问题标题】:Restricting access to a Rails Engine's routes...constraints not working限制对 Rails 引擎路由的访问...约束不起作用
【发布时间】:2011-12-06 18:42:55
【问题描述】:

我的应用中有一个 Rails 引擎。这是它与我的routes.rb 文件的挂钩:

Mercury::Engine.routes

我正在尝试限制对引擎提供的 URL 的访问,因此我尝试使用约束:

class EditorRestrictor
  def self.matches?(request)
    false
  end
end

constraints EditorRestrictor do
  Mercury::Engine.routes
end

但引擎的路线仍然可以访问。我重新启动了我的应用程序以防万一,但这没关系。有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails ruby routing rails-engines


    【解决方案1】:

    通常你会像这样为你的引擎安装路由:

    Rails.application.routes.draw do
        mount Mercury::Engine => '/mercury'
    
        # If you wanted routes mounted on root
        # mount Mercury::Engine => '/'    
    end
    

    所以如果你想添加约束,你可以说:

    Rails.application.routes.draw do
        mount Mercury::Engine => '/mercury', constraints: {}
    end
    

    【讨论】:

      猜你喜欢
      • 2012-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多