Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 轨道布线更复杂:约束_Ruby On Rails_Ruby_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 轨道布线更复杂:约束

Ruby on rails 轨道布线更复杂:约束,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,目前,如果您希望添加约束,有很多方法可以做到这一点,但正如我目前看到的,您只能包括一个最终方法,即调用。例如 Class Subdomain # Possible other `def`s here, but it's self.matches? that gets called. def self.matches?( request ) # Typical subdomain check here request.subdomain.present? &&a

目前,如果您希望添加约束,有很多方法可以做到这一点,但正如我目前看到的,您只能包括一个最终方法,即调用。例如

Class Subdomain

  # Possible other `def`s here, but it's self.matches? that gets called.

  def self.matches?( request )
    # Typical subdomain check here
    request.subdomain.present? && request.subdomain != "www"
  end

end
上述方法的问题在于它不处理www中前缀的路由,即admin和www.admin是不可分割的。可以添加更多的逻辑,但如果在一组静态子域(如admin、support和api)上需要这样做,则当前需要使SubdomainAdmin、SubdomainSupport等

这可以通过routes.rb中的正则表达式解决,如下所示:

管理员

原料药

如果请求比这更复杂,事情就会变得棘手。那么,有没有办法在用于约束的类中添加单个方法呢

基本上,如何实现以下目标?有可能吗?使用白名单子域的最佳方法是什么

例如

使用它,我们现在可以具体如下调用约束:

:constraints => Subdomain.admin_constraints
:constraints => Subdomain
所有通用约束条件如下所示:

:constraints => Subdomain.admin_constraints
:constraints => Subdomain
这在Rails 4.0.3中可能吗?

路由器将对通过路由的任何对象调用matches?request方法。就

:约束=>子域 您正在为路由提供子域类对象。但是,您也可以传递一个实例,您可以通过参数对其进行配置。e、 g

类子域 def初始化模式 @图案 终止 def匹配?请求 request.subdomain.present?&&@模式~=request.subdomain 终止 终止 routes.rb 命名空间:admin,约束:Subdomain.new/www.?admin/do 你的管理路线在这里。 终止

注意:我没有验证代码是如何工作的,我只是把它写在头顶上,所以考虑更多的是伪代码而不是实现。 此外,您可以在以下位置看到此技术的示例,以及更多详细信息:

:constraints => Subdomain